NetDataContractSerializer Constructors

Definition

Initializes a new instance of the NetDataContractSerializer class.

Overloads

NetDataContractSerializer()

Initializes a new instance of the NetDataContractSerializer class.

NetDataContractSerializer(StreamingContext)

Initializes a new instance of the NetDataContractSerializer class with the supplied streaming context data.

NetDataContractSerializer(String, String)

Initializes a new instance of the NetDataContractSerializer class with the supplied XML root element and namespace.

NetDataContractSerializer(XmlDictionaryString, XmlDictionaryString)

Initializes a new instance of the NetDataContractSerializer class with two parameters of type XmlDictionaryString that contain the root element and namespace used to specify the content.

NetDataContractSerializer(StreamingContext, Int32, Boolean, FormatterAssemblyStyle, ISurrogateSelector)

Initializes a new instance of the NetDataContractSerializer class with the supplied context data; in addition, specifies the maximum number of items in the object to be serialized, and parameters to specify whether extra data is ignored, the assembly loading method, and a surrogate selector.

NetDataContractSerializer(String, String, StreamingContext, Int32, Boolean, FormatterAssemblyStyle, ISurrogateSelector)

Initializes a new instance of the NetDataContractSerializer class with the supplied context data and root name and namespace; in addition, specifies the maximum number of items in the object to be serialized, and parameters to specify whether extra data is ignored, the assembly loading method, and a surrogate selector.

NetDataContractSerializer(XmlDictionaryString, XmlDictionaryString, StreamingContext, Int32, Boolean, FormatterAssemblyStyle, ISurrogateSelector)

Initializes a new instance of the NetDataContractSerializer class with the supplied context data, and root name and namespace (as XmlDictionaryString parameters); in addition, specifies the maximum number of items in the object to be serialized, and parameters to specify whether extra data found is ignored, assembly loading method, and a surrogate selector.

NetDataContractSerializer()

Initializes a new instance of the NetDataContractSerializer class.

public:
 NetDataContractSerializer();
public NetDataContractSerializer ();
Public Sub New ()

Examples

public static void Constructor1()
{
    // Create an instance of the NetDataContractSerializer.
    NetDataContractSerializer ser =
        new NetDataContractSerializer();
    // Other code not shown.
}
Public Shared Sub Constructor1() 
    ' Create an instance of the NetDataContractSerializer.
    Dim ser As New System.Runtime.Serialization.NetDataContractSerializer()

End Sub 

' Other code not shown.

Applies to

NetDataContractSerializer(StreamingContext)

Initializes a new instance of the NetDataContractSerializer class with the supplied streaming context data.

public:
 NetDataContractSerializer(System::Runtime::Serialization::StreamingContext context);
public NetDataContractSerializer (System.Runtime.Serialization.StreamingContext context);
new System.Runtime.Serialization.NetDataContractSerializer : System.Runtime.Serialization.StreamingContext -> System.Runtime.Serialization.NetDataContractSerializer
Public Sub New (context As StreamingContext)

Parameters

context
StreamingContext

A StreamingContext that contains context data.

Examples

The following example creates an instance of the NetDataContractSerializer using a StreamingContext.

public static void Constructor2()
{
    // Create an instance of the StreamingContext to hold
    // context data.
    StreamingContext sc = new StreamingContext
        (StreamingContextStates.CrossAppDomain);
    // Create a DatatContractSerializer with the collection.
    NetDataContractSerializer ser2 = new NetDataContractSerializer(sc);

    // Other code not shown.
}
Public Shared Sub Constructor2() 
    ' Create an instance of the StreamingContext to hold
    ' context data.
    Dim sc As New StreamingContext()
    ' Create a DatatContractSerializer with the collection.
    Dim ser2 As New System.Runtime.Serialization.NetDataContractSerializer(sc)

   ' Other code not shown.
End Sub

Remarks

The StreamingContext class allows you to pass in user-specific context data to use while reading and writing objects.

Applies to

NetDataContractSerializer(String, String)

Initializes a new instance of the NetDataContractSerializer class with the supplied XML root element and namespace.

public:
 NetDataContractSerializer(System::String ^ rootName, System::String ^ rootNamespace);
public NetDataContractSerializer (string rootName, string rootNamespace);
new System.Runtime.Serialization.NetDataContractSerializer : string * string -> System.Runtime.Serialization.NetDataContractSerializer
Public Sub New (rootName As String, rootNamespace As String)

Parameters

rootName
String

The name of the XML element that encloses the content to serialize or deserialize.

rootNamespace
String

The namespace of the XML element that encloses the content to serialize or deserialize.

Examples

The following example creates an instance of the DataContractSerializer specifying the XML root element and namespace it will expect when deserializing an object.

public static void Constructor3()
{
    // Create an instance of the NetDataContractSerializer
    // specifying the name and namespace as strings.
    NetDataContractSerializer ser =
        new NetDataContractSerializer(
        "Customer",
        "http://www.contoso.com");
    // Other code not shown.
}
Public Shared Sub Constructor3() 
    ' Create an instance of the NetDataContractSerializer
    ' specifying the name and namespace as strings.
    Dim ser As New System.Runtime.Serialization. _
       NetDataContractSerializer("Customer", "http://www.contoso.com")

   ' Other code not shown.

End Sub

Applies to

NetDataContractSerializer(XmlDictionaryString, XmlDictionaryString)

Initializes a new instance of the NetDataContractSerializer class with two parameters of type XmlDictionaryString that contain the root element and namespace used to specify the content.

public:
 NetDataContractSerializer(System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace);
public NetDataContractSerializer (System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace);
new System.Runtime.Serialization.NetDataContractSerializer : System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString -> System.Runtime.Serialization.NetDataContractSerializer
Public Sub New (rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString)

Parameters

rootName
XmlDictionaryString

An XmlDictionaryString that contains the name of the XML element that encloses the content to serialize or deserialize.

rootNamespace
XmlDictionaryString

An XmlDictionaryString that contains the namespace of the XML element that encloses the content to serialize or deserialize.

Examples

The following example creates an instance of the DataContractSerializer specifying the XML root element and namespace (as XmlDictionaryString arguments) it will expect when deserializing an object.

public static void Constructor4()
{
    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    // Initialize the out variables.
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    // Create the serializer.
    NetDataContractSerializer ser =
        new NetDataContractSerializer(
        name_value,
        ns_value);
    // Other code not shown.
}
 Public Shared Sub Constructor4() 
     ' Create an XmlDictionary and add values to it.
     Dim d As New XmlDictionary()
     Dim name_value As XmlDictionaryString =d.Add("Customer")
     Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
     
     ' Create the serializer.
     Dim ser As New System.Runtime.Serialization. _
        NetDataContractSerializer(name_value, ns_value)

     ' Other code not shown.
 
 End Sub

Remarks

The XmlDictionaryString can be used to optimize performance when the same set of strings is used across object instances.

Applies to

NetDataContractSerializer(StreamingContext, Int32, Boolean, FormatterAssemblyStyle, ISurrogateSelector)

Initializes a new instance of the NetDataContractSerializer class with the supplied context data; in addition, specifies the maximum number of items in the object to be serialized, and parameters to specify whether extra data is ignored, the assembly loading method, and a surrogate selector.

public:
 NetDataContractSerializer(System::Runtime::Serialization::StreamingContext context, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, System::Runtime::Serialization::Formatters::FormatterAssemblyStyle assemblyFormat, System::Runtime::Serialization::ISurrogateSelector ^ surrogateSelector);
public NetDataContractSerializer (System.Runtime.Serialization.StreamingContext context, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, System.Runtime.Serialization.Formatters.FormatterAssemblyStyle assemblyFormat, System.Runtime.Serialization.ISurrogateSelector surrogateSelector);
new System.Runtime.Serialization.NetDataContractSerializer : System.Runtime.Serialization.StreamingContext * int * bool * System.Runtime.Serialization.Formatters.FormatterAssemblyStyle * System.Runtime.Serialization.ISurrogateSelector -> System.Runtime.Serialization.NetDataContractSerializer
Public Sub New (context As StreamingContext, maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, assemblyFormat As FormatterAssemblyStyle, surrogateSelector As ISurrogateSelector)

Parameters

context
StreamingContext

A StreamingContext that contains context data.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type; otherwise, false.

assemblyFormat
FormatterAssemblyStyle

A FormatterAssemblyStyle enumeration value that specifies a method for locating and loading assemblies.

surrogateSelector
ISurrogateSelector

An implementation of the ISurrogateSelector.

Exceptions

maxItemsInObjectGraph value is less than 0.

Examples

The following example creates an instance of the NetDataContractSerializer using a StreamingContext. The code also sets the ignoreExtensionDataObject, and specifies an implementation of the ISurrogateSelector interface to handle to assist the serializer when selecting a surrogate (for deserializing legacy types).

public static void Constructor5()
{
    // Create an instance of the StreamingContext to hold
    // context data.
    StreamingContext sc = new StreamingContext
        (StreamingContextStates.CrossAppDomain);

    // Create an instance of a class that implements the
    // ISurrogateSelector interface. The implementation code
    // is not shown here.
    MySelector mySurrogateSelector = new MySelector();

    NetDataContractSerializer ser =
        new NetDataContractSerializer(
        sc,
        int.MaxValue,
        true,
        FormatterAssemblyStyle.Simple,
        mySurrogateSelector);

    // Other code not shown.
}
Public Shared Sub Constructor5() 
    ' Create an instance of the StreamingContext to hold
    ' context data.
    Dim sc As New StreamingContext()
    
    ' Create an instance of a class that implements the 
    ' ISurrogateSelector interface. The implementation code
    ' is not shown here.
    Dim mySurrogateSelector As New MySelector()
    
    Dim ser As New System.Runtime.Serialization. _
    NetDataContractSerializer _
    (sc, _
     65536, _
     True, _
     FormatterAssemblyStyle.Simple, _
     mySurrogateSelector)

    ' Other code not shown.
End Sub

Remarks

The ignoreExtensionDataObject parameter is used when the IExtensibleDataObject interface is implemented in the class that is being serialized or deserialized.

Applies to

NetDataContractSerializer(String, String, StreamingContext, Int32, Boolean, FormatterAssemblyStyle, ISurrogateSelector)

Initializes a new instance of the NetDataContractSerializer class with the supplied context data and root name and namespace; in addition, specifies the maximum number of items in the object to be serialized, and parameters to specify whether extra data is ignored, the assembly loading method, and a surrogate selector.

public:
 NetDataContractSerializer(System::String ^ rootName, System::String ^ rootNamespace, System::Runtime::Serialization::StreamingContext context, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, System::Runtime::Serialization::Formatters::FormatterAssemblyStyle assemblyFormat, System::Runtime::Serialization::ISurrogateSelector ^ surrogateSelector);
public NetDataContractSerializer (string rootName, string rootNamespace, System.Runtime.Serialization.StreamingContext context, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, System.Runtime.Serialization.Formatters.FormatterAssemblyStyle assemblyFormat, System.Runtime.Serialization.ISurrogateSelector surrogateSelector);
new System.Runtime.Serialization.NetDataContractSerializer : string * string * System.Runtime.Serialization.StreamingContext * int * bool * System.Runtime.Serialization.Formatters.FormatterAssemblyStyle * System.Runtime.Serialization.ISurrogateSelector -> System.Runtime.Serialization.NetDataContractSerializer
Public Sub New (rootName As String, rootNamespace As String, context As StreamingContext, maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, assemblyFormat As FormatterAssemblyStyle, surrogateSelector As ISurrogateSelector)

Parameters

rootName
String

The name of the XML element that encloses the content to serialize or deserialize.

rootNamespace
String

The namespace of the XML element that encloses the content to serialize or deserialize.

context
StreamingContext

A StreamingContext that contains context data.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type; otherwise, false.

assemblyFormat
FormatterAssemblyStyle

A FormatterAssemblyStyle enumeration value that specifies a method for locating and loading assemblies.

surrogateSelector
ISurrogateSelector

An implementation of the IDataContractSurrogate to handle the legacy type.

Exceptions

maxItemsInObjectGraph value is less than 0.

Examples

The following example creates an instance of the NetDataContractSerializer specifying the XML element name and namespace to expect when deserializing. The code also sets the ignoreExtensionDataObject, and specifies an implementation of the ISurrogateSelector interface to handle to assist the serializer when selecting a surrogate (for deserializing legacy types).

public static void Constructor6()
{
    // Create an instance of the StreamingContext to hold
    // context data.
    StreamingContext sc = new StreamingContext
        (StreamingContextStates.CrossAppDomain);

    // Create an instance of a class that implements the
    // ISurrogateSelector interface. The implementation code
    // is not shown here.
    MySelector mySurrogateSelector = new MySelector();

    NetDataContractSerializer ser =
        new NetDataContractSerializer(
        "Customer",
        "http://www.contoso.com",
        sc,
        int.MaxValue,
        true,
        FormatterAssemblyStyle.Simple,
        mySurrogateSelector);
    // Other code not shown.
}
Public Shared Sub Constructor6() 
    ' Create an instance of the StreamingContext to hold
    ' context data.
    Dim sc As New StreamingContext()
    
    ' Create an instance of a class that implements the 
    ' ISurrogateSelector interface. The implementation code
    ' is not shown here.
    Dim mySurrogateSelector As New MySelector()
    
    Dim ser As New System.Runtime.Serialization. _
      NetDataContractSerializer( _
      "Customer", _
      "http://www.contoso.com", _
      sc, _
      65536, _
      True, _
      FormatterAssemblyStyle.Simple, _
      mySurrogateSelector)

    ' Other code not shown.            

End Sub

Remarks

The ignoreExtensionDataObject parameter is used when the IExtensibleDataObject interface is implemented in the class that is being serialized or deserialized.

Applies to

NetDataContractSerializer(XmlDictionaryString, XmlDictionaryString, StreamingContext, Int32, Boolean, FormatterAssemblyStyle, ISurrogateSelector)

Initializes a new instance of the NetDataContractSerializer class with the supplied context data, and root name and namespace (as XmlDictionaryString parameters); in addition, specifies the maximum number of items in the object to be serialized, and parameters to specify whether extra data found is ignored, assembly loading method, and a surrogate selector.

public:
 NetDataContractSerializer(System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace, System::Runtime::Serialization::StreamingContext context, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, System::Runtime::Serialization::Formatters::FormatterAssemblyStyle assemblyFormat, System::Runtime::Serialization::ISurrogateSelector ^ surrogateSelector);
public NetDataContractSerializer (System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Runtime.Serialization.StreamingContext context, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, System.Runtime.Serialization.Formatters.FormatterAssemblyStyle assemblyFormat, System.Runtime.Serialization.ISurrogateSelector surrogateSelector);
new System.Runtime.Serialization.NetDataContractSerializer : System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString * System.Runtime.Serialization.StreamingContext * int * bool * System.Runtime.Serialization.Formatters.FormatterAssemblyStyle * System.Runtime.Serialization.ISurrogateSelector -> System.Runtime.Serialization.NetDataContractSerializer
Public Sub New (rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString, context As StreamingContext, maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, assemblyFormat As FormatterAssemblyStyle, surrogateSelector As ISurrogateSelector)

Parameters

rootName
XmlDictionaryString

An XmlDictionaryString that contains the root element of the content.

rootNamespace
XmlDictionaryString

An XmlDictionaryString that contains the namespace of the root element.

context
StreamingContext

A StreamingContext that contains context data.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type; otherwise, false.

assemblyFormat
FormatterAssemblyStyle

A FormatterAssemblyStyle enumeration value that specifies a method for locating and loading assemblies.

surrogateSelector
ISurrogateSelector

An implementation of the IDataContractSurrogate to handle the legacy type.

Exceptions

maxItemsInObjectGraph value is less than 0.

Examples

The following example creates an instance of the NetDataContractSerializer specifying the XML element name and namespace (as XmlDictionaryString arguments) to expect when deserializing. The code also sets the ignoreExtensionDataObject, and specifies an implementation of the ISurrogateSelector interface to handle to assist the serializer when selecting a surrogate (for deserializing legacy types).

public static void Constructor7()
{
    // Create an instance of the StreamingContext to hold
    // context data.
    StreamingContext sc = new StreamingContext
        (StreamingContextStates.CrossAppDomain);

    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    // Create an instance of a class that implements the
    // ISurrogateSelector interface. The implementation code
    // is not shown here.
    MySelector mySurrogateSelector = new MySelector();

    NetDataContractSerializer ser =
        new NetDataContractSerializer(
        name_value,
        ns_value,
        sc,
        int.MaxValue,
        true,
        FormatterAssemblyStyle.Simple,
        mySurrogateSelector);

    // Other code not shown.
}
Public Shared Sub Constructor7() 
    ' Create an instance of the StreamingContext to hold
    ' context data.
    Dim sc As New StreamingContext()
    
    ' Create an XmlDictionary and add values to it.
    Dim d As New XmlDictionary()
    Dim name_value As XmlDictionaryString =d.Add("Customer")
    Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
    
    ' Create an instance of a class that implements the 
    ' ISurrogateSelector interface. The implementation code
    ' is not shown here.
    Dim mySurrogateSelector As New MySelector()
    
    Dim ser As New System.Runtime.Serialization. _
      NetDataContractSerializer( _
      name_value, _
      ns_value, _
      sc, _
      65536, _
      True, _
      FormatterAssemblyStyle.Simple, _
      mySurrogateSelector)

    ' Other code not shown.    

End Sub

Remarks

The XmlDictionaryString can be used to optimize performance when the same set of strings is used across object instances.

The ignoreExtensionDataObject parameter is used when the IExtensibleDataObject interface is implemented in the class that is being serialized or deserialized.

Applies to