Mega Code Archive

 
Categories / C# Tutorial / XML
 

Assign XmlArrayAttribute to two arrays, and serializes a class instance that contains those arrays

using System; using System.IO; using System.Xml; using System.Xml.Serialization; public class MyClass{    [XmlArrayAttribute("MyStrings")]    public string [] MyStringArray;    [XmlArrayAttribute(ElementName = "MyIntegers")]    public int [] MyIntegerArray; } public class Run{    public static void Main()    {       Run test = new Run();       test.SerializeObject("MyClass.xml");    }    public void SerializeObject(string filename)    {       XmlSerializer s = new XmlSerializer(typeof(MyClass));       TextWriter myWriter= new StreamWriter(filename);       MyClass myClass = new MyClass();       string [] myStrings = {"Hello", "World", "!"};       myClass.MyStringArray = myStrings;       int [] myIntegers = {1,2,3};       myClass.MyIntegerArray = myIntegers;            s.Serialize(myWriter, myClass);       myWriter.Close();    } }