static void Main(string[] args) { clsPerson p = new clsPerson(); p.FirstName = "Jeff"; p.MI = "A"; p.LastName = "Price"; System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType()); x.Serialize(Console.Out, p); Console.WriteLine(); Console.ReadLine(); }
static void Main(string[] args) { clsPerson p = new clsPerson(); p.FirstName = "Jeff"; p.MI = "A"; p.LastName = "Price"; System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType()); // at this step, instead of passing Console.Out, you can pass in a // Streamwriter to write the contents to a file of your choosing. x.Serialize(Console.Out, p); Console.WriteLine(); Console.ReadLine(); }
static void Main(string[] args) { clsPerson p = new clsPerson(); p.FirstName = "John"; p.MI = "A"; p.LastName = "Smith"; TextWriter tw = new StreamWriter(@"obj.txt"); System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType()); x.Serialize(tw, p); Console.WriteLine("object written to file"); Console.ReadLine(); tw.Close(); TextReader tr = new StreamReader(@"obj.txt"); clsPerson fileP = (clsPerson)x.Deserialize(tr); Console.WriteLine("The person in the file is called " + fileP.FirstName + " " + fileP.MI + " " + fileP.LastName + "."); tr.Close(); Console.ReadLine(); }
public void TestSerializeIList () { clsPerson k = new clsPerson (); k.EmailAccounts = new ArrayList (); k.EmailAccounts.Add ("a"); k.EmailAccounts.Add ("b"); Serialize (k); Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText); }