DataContractSerializer Object
1 Public class Person2 {3 Public intId {Get;Set; }4 Public stringName {Get;Set; }5 Public intAge {Get;Set; }6 Public CharGender {Get;Set; }7}
Person
1 Private Async voidButton_Click (Objectsender, RoutedEventArgs e)2 {3Person Zhang =NewPerson ();4Zhang. Id =1;5Zhang. Name ="Letter Zhang";6Zhang. Age = -;7Zhang. Gender ='male';8 //XML Serialization Tool9DataContractSerializer serializer =NewDataContractSerializer (Zhang. GetType ());Ten using(MemoryStream ms =NewMemoryStream ()) One { A Serializer. WriteObject (MS, Zhang); -Ms. Position =0; - //reading data from a stream the using(varReader =NewStreamReader (ms)) - { - varresult =awaitReader. Readtoendasync (); - System.Diagnostics.Debug.WriteLine (result); + } - } +}
XmlSerializer Object
1 Private Async voidButton_Click (Objectsender, RoutedEventArgs e)2 {3Person Zhang =NewPerson ();4Zhang. Id =1;5Zhang. Name ="Letter Zhang";6Zhang. Age = -;7Zhang. Gender ='male';8 varSerializer =NewXmlSerializer (Zhang. GetType ());9 varSettings =NewxmlwritersettingsTen { OneIndent =true, ANewlinechars ="\ r \ n", -Encoding =Encoding.UTF8, -IndentChars =" " the }; - using(varstream =NewMemoryStream ()) - { - using(XmlWriter writer =XmlWriter.Create (stream, settings)) + { - Serializer. Serialize (writer, Zhang); + } AStream. Position =0; at using(varReader =NewStreamReader (Stream, Encoding.UTF8)) - { - varresult =awaitReader. Readtoendasync (); - System.Diagnostics.Debug.WriteLine (result); - } - } in}
Windows Phone VII, XML serialization