NetDataContractSerializer 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 NetDataContractSerializer 类的新实例。
重载
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 创建一个 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 元素的命名空间,该元素包含要序列化或反序列化的内容。
示例
下面的示例创建一个 DataContractSerializer 实例,以指定在反序列化对象时应使用的 XML 根元素和命名空间。
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
一个包含 XML 元素名称的 XmlDictionaryString,该元素包含要序列化或反序列化的内容。
- rootNamespace
- XmlDictionaryString
一个包含 XML 元素的命名空间的 XmlDictionaryString,该元素包含要序列化或反序列化的内容。
示例
下面的示例创建一个 DataContractSerializer 实例,以指定在反序列化对象时应使用的 XML 根元素和命名空间(作为 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 创建一个 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)
使用提供的上下文数据以及根名称和命名空间(作为 XmlDictionaryString 参数)初始化 NetDataContractSerializer 类的新实例;此外,还指定了对象中要序列化的最大项数、指定是否忽略找到的额外数据的参数、程序集加载方法以及代理项选择器。
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 参数。