XmlObjectSerializer Class

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 MustInherit Class XmlObjectSerializer
Dim instance As XmlObjectSerializer


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


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 Sub WriteObjectWithInstance(ByVal xm As XmlObjectSerializer, _
      ByVal graph As Company, ByVal fileName As String) 
        ' Use either the XmlDataContractSerializer or NetDataContractSerializer, 
        ' or any other class that inherits from XmlObjectSerializer to write with.
        Dim fs As New FileStream(fileName, FileMode.Create)
        Dim writer As XmlDictionaryWriter = XmlDictionaryWriter.CreateTextWriter(fs)
        xm.WriteObject(writer, graph)
        Console.WriteLine("Done writing {0}", fileName)

    End Sub  

    Private Sub Run() 
        ' Create the object to write to a file. 
        Dim graph As New Company()
        graph.Name = "cohowinery.com" 

        ' Create a DataContractSerializer and a NetDataContractSerializer. 
        ' Pass either one to the WriteObjectWithInstance method. 
        Dim dcs As New DataContractSerializer(GetType(Company))
        Dim ndcs As New NetDataContractSerializer()
        WriteObjectWithInstance(dcs, graph, "datacontract.xml")
        WriteObjectWithInstance(ndcs, graph, "netDatacontract.xml")

    End Sub 

    <DataContract()>  _
    Public Class Company
        <DataMember()>  _
        Public Name As String 
    End Class  

    Shared Sub Main() 
            Dim t As New Test()

        Catch iExc As InvalidDataContractException
            Console.WriteLine("You have an invalid data contract: ")

        Catch serExc As SerializationException
            Console.WriteLine("There is a problem with the instance:")

        Catch qExc As QuotaExceededException
            Console.WriteLine("The quota has been exceeded")
        Catch exc As Exception
        End Try 

    End Sub  
End Class

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

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

.NET Framework

Supported in: 3.5, 3.0

.NET Compact Framework

Supported in: 3.5

Community Additions

© 2015 Microsoft