How to: Read Class Data from an XML File
This example uses the Deserialize method of the XmlSerializerclass to read data that is stored in an object in a sample file that is named IntroToVCS.xml.
Example
public class Book
{
public string title;
static void Main()
{
Book introToVCS = new Book();
System.Xml.Serialization.XmlSerializer reader = new
System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
// Read the XML file.
System.IO.StreamReader file=
new System.IO.StreamReader("c:\\IntroToVCS.xml");
// Deserialize the content of the file into a Book object.
introToVCS = (Book) reader.Deserialize(file);
System.Windows.Forms.MessageBox.Show(introToVCS.title,
"Book Title");
}
}
Compiling the Code
You can compile the example directly at a command prompt, or paste the code into a console application by using the Visual Studio IDE. In the latter case, you must reference the System.Windows.Forms.dll file.
Robust Programming
The following condition(s) may cause an exception:
- The path name may be too long.
See Also
Concepts
Designing a User Interface in Visual C#