O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Load an XML Document into a Class

Problem

You want to use an XML document to persist information, but interact with the data using a custom object in your code.

Solution

Use the XmlDocument or XmlTextReader class to read XML data, and transfer it into an object. Use XmlDocument or XmlTextWriter class to persist the XML data.

Discussion

It’s common to want to work with full-fledged objects in your code and use XML only as a file format for persisting data. To support this design, you can create a class with Save and Load methods. The Save method commits the current data in the object to an XML format, whereas the Load method reads the XML document and uses its data to populate the object.

For example, the data in the orders.xml would require three classes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required