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.

C#
public NetDataContractSerializer();

Examples

C#
public static void Constructor1()
{
    // Create an instance of the NetDataContractSerializer.
    NetDataContractSerializer ser =
        new NetDataContractSerializer();
    // Other code not shown.
}

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

NetDataContractSerializer(StreamingContext)

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

C#
public NetDataContractSerializer(System.Runtime.Serialization.StreamingContext context);

Parameters

context
StreamingContext

A StreamingContext that contains context data.

Examples

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

C#
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.
}

Remarks

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

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

NetDataContractSerializer(String, String)

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

C#
public NetDataContractSerializer(string rootName, string rootNamespace);

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.

C#
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.
}

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
public NetDataContractSerializer(System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace);

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.

C#
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.
}

Remarks

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

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
public NetDataContractSerializer(System.Runtime.Serialization.StreamingContext context, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, System.Runtime.Serialization.Formatters.FormatterAssemblyStyle assemblyFormat, System.Runtime.Serialization.ISurrogateSelector surrogateSelector);

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).

C#
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.
}

Remarks

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

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
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);

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).

C#
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.
}

Remarks

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

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
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);

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).

C#
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.
}

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1