Convert Asp.net XML to JSON

Source: Internet
Author: User
Tags net xml

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  ;}}} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.