Friday, September 5, 2008

Serializing data

Scenario:
You want to serialize the data from a class

Class:

public class Address {
public string Address1;
public string Address2;
public string City;
public string State;
public string Zip;
}
XML Output:
<?xml version="1.0" encoding="utf-8"?>
<Address>
<Address1>One Microsoft Way</Address1>
<City>Redmond</City>
<State>WA</State>
<Zip>98052</Zip>
</Address>
Code:
Address oAddress = new Address();
XmlSerializer serializer = new XmlSerializer(typeof(Address));
TextWriter writer = new StreamWriter(@"c:\addresses.xml");
serializer.Serialize(writer, oAddress);
writer.Close();

0 comments: