Assign XmlArrayAttribute to two arrays, and serializes a class instance that contains those arrays. : Xml serialization « XML « C# / CSharp Tutorial






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();
   }
}








30.30.Xml serialization
30.30.1.XML serialization with namespace setting
30.30.2.Serialize/Deserialize Xml: deal with element list
30.30.3.Specify the XmlRoot and XmlAttribute for XML serialization
30.30.4.Specify format and indentation for object XML serialization
30.30.5.Using XmlSerializer to Serialize a Linq object
30.30.6.XML Serialization Sample
30.30.7.Serialization of an object marked with XmlAttribute and XmlIgnore
30.30.8.Serialize a list of object to Xml with Linq
30.30.9.Xml Serialization for Enum
30.30.10.Xml Serialization for DateTime value
30.30.11.Assign XmlArrayAttribute to two arrays, and serializes a class instance that contains those arrays.
30.30.12.XmlTextAttribute with type string informs the XmlSerializer that strings should be serialized as XML text