[C#] XmlSerializer example

// This is the test class we want to 
// serialize:
public class TestClass
    private string someString;
    public string SomeString
        get { return someString; }
        set { someString = value; }
    private List<string> settings = new List<string>();
    public List<string> Settings
        get { return settings; }
        set { settings = value; }
    // These will be ignored
    private int willBeIgnored1 = 1;
    private int willBeIgnored2 = 1;
// Example code
// This example requires:
// using System.Xml.Serialization;
// using System.IO;
// Create a new instance of the test class
TestClass TestObj = new TestClass();
// Set some dummy values
TestObj.SomeString = "foo";
#region Save the object
// Create a new XmlSerializer instance with the type of the test class
XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(@"C:\test.xml");
SerializerObj.Serialize(WriteFileStream, TestObj);
// Cleanup
The test.xml file will look like this:
<?xml version="1.0"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
#region Load the object
// Create a new file stream for reading the XML file
FileStream ReadFileStream = new FileStream(@"C:\test.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
// Load the object saved above by using the Deserialize function
TestClass LoadedObj = (TestClass)SerializerObj.Deserialize(ReadFileStream);
// Cleanup
// Test the new loaded object:
foreach (string Setting in LoadedObj.Settings)

더보기 소개 [C#] XmlSerializer example