NetDataContractSerializer 构造函数

定义

初始化 NetDataContractSerializer 类的新实例。

重载

名称 说明
NetDataContractSerializer()

初始化 NetDataContractSerializer 类的新实例。

NetDataContractSerializer(StreamingContext)

使用提供的流式处理上下文数据初始化类的新实例 NetDataContractSerializer

NetDataContractSerializer(String, String)

使用提供的 XML 根元素和命名空间初始化类的新实例 NetDataContractSerializer

NetDataContractSerializer(XmlDictionaryString, XmlDictionaryString)

使用包含用于指定内容的根元素和命名空间的两个参数NetDataContractSerializer初始化类的新实例XmlDictionaryString

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

使用提供的上下文数据初始化类的新实例 NetDataContractSerializer ;此外,指定对象中要序列化的最大项数,以及用于指定是否忽略额外数据的参数、程序集加载方法和代理项选择器。

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

使用提供的上下文数据和根名称和命名空间初始化类的新实例 NetDataContractSerializer ;此外,还指定要序列化的对象中的最大项数,以及用于指定是否忽略额外数据的参数、程序集加载方法和代理项选择器。

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

使用提供的上下文数据和根名称和命名空间(作为NetDataContractSerializer参数)初始化类的新实例XmlDictionaryString;此外,还指定要序列化的对象中的最大项数,以及用于指定是否忽略额外数据、程序集加载方法和代理项选择器的参数。

NetDataContractSerializer()

初始化 NetDataContractSerializer 类的新实例。

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

示例

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.

适用于

NetDataContractSerializer(StreamingContext)

使用提供的流式处理上下文数据初始化类的新实例 NetDataContractSerializer

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)

参数

context
StreamingContext

一个 StreamingContext 包含上下文数据。

示例

以下示例创建 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

注解

StreamingContext 类允许在读取和写入对象时传入用户特定的上下文数据。

适用于

NetDataContractSerializer(String, String)

使用提供的 XML 根元素和命名空间初始化类的新实例 NetDataContractSerializer

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)

参数

rootName
String

包含要序列化或反序列化的内容的 XML 元素的名称。

rootNamespace
String

包含要序列化或反序列化的内容的 XML 元素的命名空间。

示例

以下示例创建一个指定 XML 根元素的实例 DataContractSerializer ,并在反序列化对象时需要命名空间。

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

适用于

NetDataContractSerializer(XmlDictionaryString, XmlDictionaryString)

使用包含用于指定内容的根元素和命名空间的两个参数NetDataContractSerializer初始化类的新实例XmlDictionaryString

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)

参数

rootName
XmlDictionaryString

一个 XmlDictionaryString 包含 XML 元素的名称,该元素包含要序列化或反序列化的内容。

rootNamespace
XmlDictionaryString

一个 XmlDictionaryString 包含 XML 元素的命名空间,该命名空间包含要序列化或反序列化的内容。

示例

以下示例创建一个指定 XML 根元素和命名空间(作为DataContractSerializer参数)的实例,该实例XmlDictionaryString在反序列化对象时预期。

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

注解

XmlDictionaryString当在同一组字符串跨对象实例使用时,可用于优化性能。

适用于

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

使用提供的上下文数据初始化类的新实例 NetDataContractSerializer ;此外,指定对象中要序列化的最大项数,以及用于指定是否忽略额外数据的参数、程序集加载方法和代理项选择器。

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)

参数

context
StreamingContext

一个 StreamingContext 包含上下文数据。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。

ignoreExtensionDataObject
Boolean

true 如果忽略类型扩展提供的数据,则为 ;否则,为 false.

assemblyFormat
FormatterAssemblyStyle

一个 FormatterAssemblyStyle 枚举值,该值指定用于查找和加载程序集的方法。

surrogateSelector
ISurrogateSelector

ISurrogateSelector实现。

例外

maxItemsInObjectGraph 值小于 0。

示例

以下示例创建 NetDataContractSerializer using a StreamingContext. 该代码还设置 ignoreExtensionDataObject并指定接口的 ISurrogateSelector 实现,以便在选择代理项(用于反序列化旧类型)时协助序列化程序。

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

注解

ignoreExtensionDataObject 正在序列化或反序列化的类中实现接口时 IExtensibleDataObject ,将使用该参数。

适用于

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

使用提供的上下文数据和根名称和命名空间初始化类的新实例 NetDataContractSerializer ;此外,还指定要序列化的对象中的最大项数,以及用于指定是否忽略额外数据的参数、程序集加载方法和代理项选择器。

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)

参数

rootName
String

包含要序列化或反序列化的内容的 XML 元素的名称。

rootNamespace
String

包含要序列化或反序列化的内容的 XML 元素的命名空间。

context
StreamingContext

一个 StreamingContext 包含上下文数据。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。

ignoreExtensionDataObject
Boolean

true 如果忽略类型扩展提供的数据,则为 ;否则,为 false.

assemblyFormat
FormatterAssemblyStyle

一个 FormatterAssemblyStyle 枚举值,该值指定用于查找和加载程序集的方法。

surrogateSelector
ISurrogateSelector

要处理旧类型的实现 IDataContractSurrogate

例外

maxItemsInObjectGraph 值小于 0。

示例

以下示例创建一个实例, NetDataContractSerializer 用于指定反序列化时预期的 XML 元素名称和命名空间。 该代码还设置 ignoreExtensionDataObject并指定接口的 ISurrogateSelector 实现,以便在选择代理项(用于反序列化旧类型)时协助序列化程序。

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

注解

ignoreExtensionDataObject 正在序列化或反序列化的类中实现接口时 IExtensibleDataObject ,将使用该参数。

适用于

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

使用提供的上下文数据和根名称和命名空间(作为NetDataContractSerializer参数)初始化类的新实例XmlDictionaryString;此外,还指定要序列化的对象中的最大项数,以及用于指定是否忽略额外数据、程序集加载方法和代理项选择器的参数。

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)

参数

rootName
XmlDictionaryString

一个 XmlDictionaryString 包含内容的根元素。

rootNamespace
XmlDictionaryString

一个 XmlDictionaryString 包含根元素的命名空间。

context
StreamingContext

一个 StreamingContext 包含上下文数据。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。

ignoreExtensionDataObject
Boolean

true 如果忽略类型扩展提供的数据,则为 ;否则,为 false.

assemblyFormat
FormatterAssemblyStyle

一个 FormatterAssemblyStyle 枚举值,该值指定用于查找和加载程序集的方法。

surrogateSelector
ISurrogateSelector

要处理旧类型的实现 IDataContractSurrogate

例外

maxItemsInObjectGraph 值小于 0。

示例

以下示例创建一个实例 NetDataContractSerializer ,用于指定反序列化时所需的 XML 元素名称和命名空间(作为 XmlDictionaryString 参数)。 该代码还设置 ignoreExtensionDataObject并指定接口的 ISurrogateSelector 实现,以便在选择代理项(用于反序列化旧类型)时协助序列化程序。

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

注解

XmlDictionaryString当在同一组字符串跨对象实例使用时,可用于优化性能。

ignoreExtensionDataObject 正在序列化或反序列化的类中实现接口时 IExtensibleDataObject ,将使用该参数。

适用于