NetDataContractSerializer 建構函式

定義

初始化 NetDataContractSerializer 類別的新執行個體。

多載

名稱 Description
NetDataContractSerializer()

初始化 NetDataContractSerializer 類別的新執行個體。

NetDataContractSerializer(StreamingContext)

用提供的串流上下文資料初始化類別的新實例 NetDataContractSerializer

NetDataContractSerializer(String, String)

初始化一個新的類別實例 NetDataContractSerializer ,使用提供的 XML 根元素與命名空間。

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

包含上下文資料的 A 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)

初始化一個新的類別實例 NetDataContractSerializer ,使用提供的 XML 根元素與命名空間。

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

一個 XmlDictionaryString 包含包含內容的XML元素名稱的函數,該元素用於序列化或反序列化內容。

rootNamespace
XmlDictionaryString

一個 XmlDictionaryString 包含包含內容的XML元素命名空間,該元素可進行序列化或反序列化內容。

範例

以下範例建立一個實例, 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

包含上下文資料的 A 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

包含上下文資料的 A 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

包含上下文資料的 A 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使用此參數。

適用於