共用方式為


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)

使用提供的內容資料以及根名稱和命名空間 (相同於 XmlDictionaryString 參數的處理方式),初始化 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

範例

下列範例會建立使用 NetDataContractSerializerStreamingContext 執行個體。

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

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

包含內容資料的 StreamingContext

maxItemsInObjectGraph
Int32

圖形中要序列化或還原序列化的最大項目數。

ignoreExtensionDataObject
Boolean

如果要略過型別延伸所提供的資料,則為 true,否則為 false

assemblyFormat
FormatterAssemblyStyle

FormatterAssemblyStyle 列舉值,指定用來尋找及載入組件的方法。

surrogateSelector
ISurrogateSelector

ISurrogateSelector 的實作。

例外狀況

maxItemsInObjectGraph 值小於 0。

範例

下列範例會建立使用 NetDataContractSerializerStreamingContext 執行個體。 這段程式碼也會設定 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 參數。

適用於