XmlSerializable Sample

Back to summary

Source

using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace XmlSerializersTest.Samples
{
    public class XmlSerializable : IXmlSerializable
    {
        public string Field { get; set; }
        const string prefix = "custom: ";

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            var temp = reader.ReadString();
            if (temp.StartsWith(prefix))
            {
                Field = temp.Substring(prefix.Length);
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteString(prefix + Field);
        }
    }
}

Standard Serializer

<XmlSerializable>custom: test</XmlSerializable>

XAML Serializer

<XmlSerializable Field="test" xmlns="clr-namespace:XmlSerializersTest.Samples;assembly=XmlSerializersTest" />

WCF Serializer

<XmlSerializable xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">custom: test</XmlSerializable>

WCF-ref Serializer

<XmlSerializable xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">custom: test</XmlSerializable>