XmlTextAttribute with type string informs the XmlSerializer that strings should be serialized as XML text : Xml serialization « XML « C# / CSharp Tutorial






using System;
using System.Xml.Serialization;
using System.IO;


public class MyValue1
{
    [XmlText(typeof(string))]
    [XmlElement(typeof(int))]
    [XmlElement(typeof(double))]
    public object[] All = new object[] { 321, "One", 2, 3.0, "Two" };
}

public class MyValue2
{
    [XmlText(Type = typeof(MyValueType))]
    public MyValueType Type;
}
public enum MyValueType
{
    Small,
    Medium,
    Large
}

public class MyValue3
{
    [XmlText(Type = typeof(DateTime))]
    public DateTime CreationTime = DateTime.Now;
}

public class Test
{
    static void Main()
    {
        Test t = new Test();
        t.SerializeArray("XmlText1.xml");
    }

    private void SerializeArray(string filename)
    {
        XmlSerializer ser = new XmlSerializer(typeof(MyValue1));
        MyValue1 myValue1 = new MyValue1();

        TextWriter writer = new StreamWriter(filename);

        ser.Serialize(writer, myValue1);
        writer.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