Friday, September 5, 2008

Deserializing XML file

Scenario:
You want to serialize the data from a class

XML:


<?xml version="1.0" encoding="utf-8"?>
<Address>
<Address1>One Microsoft Way</Address1>
<City>Redmond</City>
<State>WA</State>
<Zip>98052</Zip>
</Address>

Class:

public class Address {
public string Address1;
public string Address2;
public string City;
public string State;
public string Zip;
}
Code:
Address oAddress = new Address();
XmlSerializer serializer = new XmlSerializer(typeof(Address));
FileStream file = new FileStream(@"c:\address.xml", FileMode.Open);
oAddress = serializer.Deserialize(file) as Address;

0 comments: