NetDataContractSerializer Class

Serializes and deserializes an instance of a type into XML stream or document using the supplied .NET Framework types. This class cannot be inherited.

Namespace: System.Runtime.Serialization
Assembly: System.Runtime.Serialization (in system.runtime.serialization.dll)

public sealed class NetDataContractSerializer : XmlObjectSerializer, IFormatter
public final class NetDataContractSerializer extends XmlObjectSerializer implements IFormatter
public final class NetDataContractSerializer extends XmlObjectSerializer implements IFormatter
Not applicable.

The NetDataContractSerializer differs from the DataContractSerializer in one important way: the NetDataContractSerializer includes CLR type information in the serialized XML, whereas the DataContractSerializer does not. Therefore, the NetDataContractSerializer can be used only if both the serializing and deserializing ends share the same CLR types.

The serializer can serialize types to which either the DataContractAttribute or SerializableAttribute attribute has been applied. It also serializes types that implement ISerializable.

For more information about serialization, see Using Stand-alone Serialization.

The following example code shows a type named Person that is serialized by the NetDataContractSerializer. The DataContractAttribute attribute is applied to the class, and the DataMemberAttribute is applied to members (including a private member) to instruct the NetDataContractSerializer what to serialize.

// You must apply a DataContractAttribute or SerializableAttribute
// to a class to have it serialized by the NetDataContractSerializer.
[DataContract(Name = "Customer", Namespace = "")]
class Person : IExtensibleDataObject
    public string FirstName;
    public string LastName;
    public int ID;

    public Person(string newfName, string newLName, int newID)
        FirstName = newfName;
        LastName = newLName;
        ID = newID;

    private ExtensionDataObject extensionData_Value;

    public ExtensionDataObject ExtensionData
            return extensionData_Value;
            extensionData_Value = value;

public sealed class Test
    private Test() { }

    public static void Main()

        catch (SerializationException serExc)
            Console.WriteLine("Serialization Failed");
        catch (Exception exc)
            "The serialization operation failed: {0} StackTrace: {1}",
            exc.Message, exc.StackTrace);

            Console.WriteLine("Press <Enter> to exit....");

    public static void WriteObject(string fileName)
            "Creating a Person object and serializing it.");
        Person p1 = new Person("Zighetti", "Barbara", 101);
        FileStream fs = new FileStream(fileName, FileMode.Create);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        NetDataContractSerializer ser =
            new NetDataContractSerializer();
        ser.WriteObject(writer, p1);

    public static void ReadObject(string fileName)
        Console.WriteLine("Deserializing an instance of the object.");
        FileStream fs = new FileStream(fileName,
        XmlDictionaryReader reader =
            XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
        NetDataContractSerializer ser = new NetDataContractSerializer();

        // Deserialize the data and read it from the instance.
        Person deserializedPerson =
            (Person)ser.ReadObject(reader, true);
        Console.WriteLine(String.Format("{0} {1}, ID: {2}",
        deserializedPerson.FirstName, deserializedPerson.LastName,


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 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions