XmlObjectSerializer Class

Provides the base class used to serialize objects as XML streams or documents. This class is abstract.

Namespace:  System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)
public abstract class XmlObjectSerializer

The XmlObjectSerializer type exposes the following members.

  NameDescription
Protected methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsXmlObjectSerializerInitializes a new instance of the XmlObjectSerializer class.
Top
  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsStartObject(XmlDictionaryReader)Gets a value that specifies whether the XmlDictionaryReader is positioned over an XML element that can be read.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsIsStartObject(XmlReader)Gets a value that specifies whether the XmlReader is positioned over an XML element that can be read.
Protected methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsReadObject(Stream)Reads the XML stream or document with a Stream and returns the deserialized object.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsReadObject(XmlDictionaryReader)Reads the XML document or stream with an XmlDictionaryReader and returns the deserialized object.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsReadObject(XmlReader)Reads the XML document or stream with an XmlReader and returns the deserialized object.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsReadObject(XmlDictionaryReader, Boolean)Reads the XML stream or document with an XmlDictionaryReader and returns the deserialized object; it also enables you to specify whether the serializer can read the data before attempting to read it.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsReadObject(XmlReader, Boolean)Reads the XML document or stream with an XmlReader and returns the deserialized object; it also enables you to specify whether the serializer can read the data before attempting to read it.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteEndObject(XmlDictionaryWriter)Writes the end of the object data as a closing XML element to the XML document or stream with an XmlDictionaryWriter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteEndObject(XmlWriter)Writes the end of the object data as a closing XML element to the XML document or stream with an XmlWriter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteObject(Stream, Object)Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified Stream.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteObject(XmlDictionaryWriter, Object)Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified XmlDictionaryWriter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteObject(XmlWriter, Object)Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified XmlWriter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteObjectContent(XmlDictionaryWriter, Object)Writes only the content of the object to the XML document or stream using the specified XmlDictionaryWriter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteObjectContent(XmlWriter, Object)Writes only the content of the object to the XML document or stream with the specified XmlWriter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteStartObject(XmlDictionaryWriter, Object)Writes the start of the object's data as an opening XML element using the specified XmlDictionaryWriter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWriteStartObject(XmlWriter, Object)Writes the start of the object's data as an opening XML element using the specified XmlWriter.
Top
ExceptionCondition
InvalidDataContractException

the type being serialized does not conform to data contract rules. For example, the DataContractAttribute attribute has not been applied to the type.

SerializationException

there is a problem with the instance being serialized.

Extend the XmlObjectSerializer to create your own serializer to serialize and deserialize objects. Both the DataContractSerializer class and the NetDataContractSerializer class inherit from the XmlObjectSerializer and are used to serialize and deserialize objects that conform to data contract rules (objects created using the DataContractAttribute and the DataMemberAttribute).

Notes to Inheritors

When you inherit from XmlObjectSerializer, you must override the following members: XmlObjectSerializer.WriteStartObject(XmlDictionaryWriter, Object), XmlObjectSerializer.WriteObjectContent(XmlDictionaryWriter, Object), XmlObjectSerializer.WriteEndObject(XmlDictionaryWriter). Additionally, the IsStartObject and ReadObject methods must be implemented for reading and deserializing.

The following example shows a method named WriteObjectWithInstance that includes an XmlObjectSerializer as a parameter. The method serializes an object using either the DataContractSerializer or NetDataContractSerializer by calling the WriteObject method.

public class Test
{
    private void WriteObjectWithInstance(XmlObjectSerializer xm, Company graph,
       string fileName)
    {
        // Use either the XmlDataContractSerializer or NetDataContractSerializer, 
        // or any other class that inherits from XmlObjectSerializer to write with.
        Console.WriteLine(xm.GetType());
        FileStream fs = new FileStream(fileName, FileMode.Create);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        xm.WriteObject(writer, graph);
        Console.WriteLine("Done writing {0}", fileName);
    }

    private void Run()
    {
        // Create the object to write to a file.
        Company graph = new Company();
        graph.Name = "cohowinery.com";

        // Create a DataContractSerializer and a NetDataContractSerializer. 
        // Pass either one to the WriteObjectWithInstance method.
        DataContractSerializer dcs = new DataContractSerializer(typeof(Company));
        NetDataContractSerializer ndcs = new NetDataContractSerializer();
        WriteObjectWithInstance(dcs, graph, @"datacontract.xml");
        WriteObjectWithInstance(ndcs, graph, @"netDatacontract.xml");
    }

    [DataContract]
    public class Company
    {
        [DataMember]
        public string Name;
    }

    static void Main()
    {
        try
        {
            Console.WriteLine("Starting");
            Test t = new Test();
            t.Run();
            Console.WriteLine("Done");
            Console.ReadLine();
        }

        catch (InvalidDataContractException iExc)
        {
            Console.WriteLine("You have an invalid data contract: ");
            Console.WriteLine(iExc.Message);
            Console.ReadLine();
        }

        catch (SerializationException serExc)
        {
            Console.WriteLine("There is a problem with the instance:");
            Console.WriteLine(serExc.Message);
            Console.ReadLine();
        }

        catch (QuotaExceededException qExc)
        {
            Console.WriteLine("The quota has been exceeded");
            Console.WriteLine(qExc.Message);
            Console.ReadLine();
        }
        catch (Exception exc)
        {
            Console.WriteLine(exc.Message);
            Console.WriteLine(exc.ToString());
            Console.ReadLine();
        }
    }

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft. All rights reserved.