XML serialization and deserialization

Source: Internet
Author: User

The XML format is as follows:

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Persons  >      <  Person  Name  = "James"  Sex  = "Male"  Age  = "18"  Height  = "180"  > </  Person  >      <  Person  Name  = "Li Si"  Sex  = "Male"  Age  = "21"  Height  = "170"   > </  Person  >      < Person  Name  = "Wang Wu"  Sex  = "Female"  Age  = "12"  Height  = "120"   > </  Person  >      <  Person  Name  = "Zhao Liu"  Sex = "Male"  Age  = "58"  Height  = "160"   >          <  Children  >              <  Child  Name  = "CHILD 1"  Sex  = "2"  > </  Child >              <  Child  Name  = "Child 2"  Sex  = "5"  > </  Child  >          </  Children  >      </  Person  >  </ Persons  > 

For the sake of convenience, the following will slightly read the XML file and write it into the production XML file, and directly write the above XML file into the CS file.

 Namespace  XML serialization {  Class  Myxmloptions {  Public Persons per { Get ; Set  ;}  Public   String Xmlstring { Get ; Set ;}  Public  Myxmloptions (){  //  Read local XML file content Xmlstring = "  <? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?>  " + "  <Persons>  " + "  <Person name = \ "James \" Sex = \ "male \" age = \ "18 \" Height = \ "180 \"> </person> " + "  <Person name = \ "\" Sex = \ "male \" age = \ "21 \" Height = \ "170 \"> </person>  " + "  <Person name = \ "\" Sex = \ "female \" age = \ "12 \" Height = \ "120 \"> </person>  " + "  <Person name = \ "Zhao \" Sex = \ "male \" age = \ "58 \" Height = \ "160 \">  " + "  <Children> " + "  <Child name = \ "CHILD 1 \" Sex = \ "2 \"> </child>  " + "  <Child name = \ "Child 2 \" Sex = \ "5 \"> </child>  " + "  </Children>  " + "  </Person>  " + " </Persons>  "  ;}  Public   String  Xmldeserialize (){  If (Per! = Null  ) {Stringbuilder sb = New Stringbuilder ( 5000  ); Xmlserializer Ser = New Xmlserializer (Typeof  (Persons ));  Using (Textwriter writer = New  Stringwriter (SB) {Ser. serialize (writer, per );  Return  Writer. tostring ();}}  Else  {  Return   "  Object does not exist. Rice serialization fails.  " ;}}  Public   Void  Xmlserialize () {xmlserializer Ser = New Xmlserializer ( Typeof  (Persons ));  Using (Textreader reader = New  Stringreader (xmlstring) {per = (Persons) SER. deserialize (Reader );}}}} 

ClientCodeAs follows:

 Static VoidMain (String[] ARGs) {myxmloptions Option=NewMyxmloptions (); option. xmlserialize (); console. writeline (option. xmlstring); console. writeline (option. xmldeserialize (); console. readkey ();}

To enable XML to be serialized as object objects and deserialized, The poersons object is set as follows based on the XML structure:

 Namespace  XML serialization {[xmlroot (  "  Persons  " , Isnullable = False )]  Public   Class  Persons {[xmlelement (  "  Person  "  )]  Public List <person> Person ;}  Public   Class  Person {[xmlattribute (  "  Name  " )]  Public   String Name { Get ; Set  ;} [Xmlattribute (  "  Sex  "  )]  Public   String Sex { Get ; Set  ;} [Xmlattribute ( "  Age  "  )]  Public   String Age { Get ; Set  ;} [Xmlattribute (  "  Height  "  )]  Public   String Height { Get ;Set  ;} [Xmlelement (  "  Children  "  )]  Public  Children ;}  Public   Class  Children {[xmlelement (  "  Child  "  )]  Public List <child>Child ;}  Public   Class  Child {[xmlattribute (  "  Name  "  )]  Public   String Name { Get ; Set  ;} [Xmlattribute (  "  Sex  " )]  Public   String Sex { Get ; Set  ;}}} 

 

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.