First query the XML node with LINQ, convert it to list or model, and then convert it to JSON with JSON. net.
The advantage is that the list can be returned at any time.
XML
<? XML version = "1.0" encoding = "UTF-8" ?> < Users > < User ID = "111111" > < Name > Ericsun</ Name > < Password > 123456 </ Password > < Description > Hello I'm from Dalian </ Description > </ User > < User ID = "222222" > < Name > Ray </ Name > < Password > 654321 </ Password > < Description > Hello I'm from Jilin </ Description > </ User > </ Users >
C #Code
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. xml. LINQ; Using Newtonsoft. JSON; Namespace Leleapplication1 { Class Program { Static Void Main (String [] ARGs) {list <User> List = New List <user> (); List <User> list2 = New List <user> (); Xelement rootnode = Xelement. Load ( @" D: \ 234.xml " ); // First Query VaR Mynode = (From R In Rootnode. descendants ( " User " ) Select R). tolist (); Foreach ( VaR Item In Mynode) {user model = New User (); Model. Name = Item. element ( " Name " ). Value; model. Password = Item. element ( " Password " ). Value; model. Description = Item. element ( " Description " ). Value; List. Add (model );} // End of the first query // Convert to JSON String JSON = Jsonconvert. serializeobject (list ); // Second Query VaR Mynode2 = From R2 In Rootnode. descendants ( " User " ) Select New {Name = R2.element ( " Name " ). Value, password = R2.element ( " Password " ). Value, description = R2.element ( " Description " ). Value }; Foreach ( VaR Item In Mynode2) {user model = New User (); Model. Name = Item. Name; model. Password = Item. Password; model. Description = Item. description; list2.add (model );} // The second query ends. // Convert to JSON String Json2 = Jsonconvert. serializeobject (list2); console. readkey ();}} Public Class User { Public String Name { Get ; Set ;} Public String Password { Get ;Set ;} Public String Description { Get ; Set ;}}}