Reading Xml Into Objects

(Visual Studio 2005 and above)

You probably already have some xml you want to read. Use xsd.exe to generate xsd from the xml:

"c:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" your.xml

It will generate your.xsd in the current directory. Generate code to parse xml as follows:

"c:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe" your.xsd /classes /out:<dir_to_put_code>

It will generate your.cs in the specified directory. Include that file in your project. Reading xml can then be done in two ways:

Reading xml from file

TextReader reader = new StreamReader("your.xml");
XmlSerializer serializer = new XmlSerializer(typeof(your));
your y = (your)serializer.Deserialize(reader);

Reading xml from string

StringReader reader = new StringReader("string containing xml");
XmlSerializer serializer = new XmlSerializer(typeof(your));
your y = (your)serializer.Deserialize(reader);

page_revision: 2, last_edited: 1241615128|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License