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