XmlObjectSerializer 類別

定義

提供用於將物件序列化為 XML 串流或文件的基底類別。 這個類別是抽象的。

public ref class XmlObjectSerializer abstract
public abstract class XmlObjectSerializer
type XmlObjectSerializer = class
Public MustInherit Class XmlObjectSerializer
繼承
XmlObjectSerializer
衍生

範例

以下範例展示了一個名為 WriteObjectWithInstance 的方法,其中包含 參數 XmlObjectSerializer 。 該方法透過呼叫該WriteObject方法,使用 或 DataContractSerializerNetDataContractSerializer 序列化物件。

public class Test
{
    private void WriteObjectWithInstance(XmlObjectSerializer xm, Company graph,
       string fileName)
    {
        // Use either the XmlDataContractSerializer or NetDataContractSerializer,
        // or any other class that inherits from XmlObjectSerializer to write with.
        Console.WriteLine(xm.GetType());
        FileStream fs = new FileStream(fileName, FileMode.Create);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        xm.WriteObject(writer, graph);
        Console.WriteLine("Done writing {0}", fileName);
    }

    private void Run()
    {
        // Create the object to write to a file.
        Company graph = new Company();
        graph.Name = "cohowinery.com";

        // Create a DataContractSerializer and a NetDataContractSerializer.
        // Pass either one to the WriteObjectWithInstance method.
        DataContractSerializer dcs = new DataContractSerializer(typeof(Company));
        NetDataContractSerializer ndcs = new NetDataContractSerializer();
        WriteObjectWithInstance(dcs, graph, @"datacontract.xml");
        WriteObjectWithInstance(ndcs, graph, @"netDatacontract.xml");
    }

    [DataContract]
    public class Company
    {
        [DataMember]
        public string Name;
    }

    static void Main()
    {
        try
        {
            Console.WriteLine("Starting");
            Test t = new Test();
            t.Run();
            Console.WriteLine("Done");
            Console.ReadLine();
        }

        catch (InvalidDataContractException iExc)
        {
            Console.WriteLine("You have an invalid data contract: ");
            Console.WriteLine(iExc.Message);
            Console.ReadLine();
        }

        catch (SerializationException serExc)
        {
            Console.WriteLine("There is a problem with the instance:");
            Console.WriteLine(serExc.Message);
            Console.ReadLine();
        }

        catch (QuotaExceededException qExc)
        {
            Console.WriteLine("The quota has been exceeded");
            Console.WriteLine(qExc.Message);
            Console.ReadLine();
        }
        catch (Exception exc)
        {
            Console.WriteLine(exc.Message);
            Console.WriteLine(exc.ToString());
            Console.ReadLine();
        }
    }
Public Class Test
    
    Private Sub WriteObjectWithInstance(ByVal xm As XmlObjectSerializer, _
      ByVal graph As Company, ByVal fileName As String) 
        ' Use either the XmlDataContractSerializer or NetDataContractSerializer,
        ' or any other class that inherits from XmlObjectSerializer to write with.
        Console.WriteLine(xm.GetType())
        Dim fs As New FileStream(fileName, FileMode.Create)
        Dim writer As XmlDictionaryWriter = XmlDictionaryWriter.CreateTextWriter(fs)
        xm.WriteObject(writer, graph)
        Console.WriteLine("Done writing {0}", fileName)
    
    End Sub 
    
    
    Private Sub Run() 
        ' Create the object to write to a file.
        Dim graph As New Company()
        graph.Name = "cohowinery.com"
        
        ' Create a DataContractSerializer and a NetDataContractSerializer.
        ' Pass either one to the WriteObjectWithInstance method.
        Dim dcs As New DataContractSerializer(GetType(Company))
        Dim ndcs As New NetDataContractSerializer()
        WriteObjectWithInstance(dcs, graph, "datacontract.xml")
        WriteObjectWithInstance(ndcs, graph, "netDatacontract.xml")
    
    End Sub 
    
    <DataContract()>  _
    Public Class Company
        <DataMember()>  _
        Public Name As String
    End Class 
    
    
    Shared Sub Main() 
        Try
            Console.WriteLine("Starting")
            Dim t As New Test()
            t.Run()
            Console.WriteLine("Done")
            Console.ReadLine()
        
        Catch iExc As InvalidDataContractException
            Console.WriteLine("You have an invalid data contract: ")
            Console.WriteLine(iExc.Message)
            Console.ReadLine()
        
        Catch serExc As SerializationException
            Console.WriteLine("There is a problem with the instance:")
            Console.WriteLine(serExc.Message)
            Console.ReadLine()
        
        Catch qExc As QuotaExceededException
            Console.WriteLine("The quota has been exceeded")
            Console.WriteLine(qExc.Message)
            Console.ReadLine()
        Catch exc As Exception
            Console.WriteLine(exc.Message)
            Console.WriteLine(exc.ToString())
            Console.ReadLine()
        End Try
    
    End Sub 
End Class

備註

擴充 以 XmlObjectSerializer 建立你自己的序列化器,來序列化和反序列化物件。 DataContractSerializer類別與NetDataContractSerializer類別都繼承自 XmlObjectSerializer 和 ,用於序列化與反序列化符合資料契約規則的物件(使用 與 DataContractAttributeDataMemberAttribute建立的物件)。

給實施者的注意事項

當你從 繼承 XmlObjectSerializer時,必須覆蓋以下成員: WriteStartObject(XmlDictionaryWriter, Object)WriteObjectContent(XmlDictionaryWriter, Object)WriteEndObject(XmlDictionaryWriter)。 此外, IsStartObject 還必須實作 and ReadObject 方法來進行讀取與反序列化。

建構函式

名稱 Description
XmlObjectSerializer()

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

方法

名稱 Description
Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsStartObject(XmlDictionaryReader)

會得到一個值,指定是否 XmlDictionaryReader 位於可讀取的 XML 元素上。

IsStartObject(XmlReader)

會得到一個值,指定是否 XmlReader 位於可讀取的 XML 元素上。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ReadObject(Stream)

讀取帶有 的 Stream XML 串流或文件,並回傳反序列化的物件。

ReadObject(XmlDictionaryReader, Boolean)

讀取 XML 串流或文件 XmlDictionaryReader ,並回傳反序列化的物件;同時也允許你在嘗試讀取資料前指定序列化器是否能讀取資料。

ReadObject(XmlDictionaryReader)

讀取 XML 文件或串流, XmlDictionaryReader 並回傳反序列化的物件。

ReadObject(XmlReader, Boolean)

讀取 XML 文件或串流, XmlReader 並回傳反序列化的物件;同時也允許你在嘗試讀取資料前指定序列化器是否能讀取資料。

ReadObject(XmlReader)

讀取 XML 文件或串流, XmlReader 並回傳反序列化的物件。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
WriteEndObject(XmlDictionaryWriter)

將物件資料的結尾寫入 XML 文件或串流的閉尾 XML 元素,並帶有 XmlDictionaryWriter

WriteEndObject(XmlWriter)

將物件資料的結尾寫入 XML 文件或串流的閉尾 XML 元素,並帶有 XmlWriter

WriteObject(Stream, Object)

將物件的完整內容(起始、內容及結束)寫入 XML 文件或串流,並指定為 Stream

WriteObject(XmlDictionaryWriter, Object)

將物件的完整內容(起始、內容及結束)寫入 XML 文件或串流,並指定為 XmlDictionaryWriter

WriteObject(XmlWriter, Object)

將物件的完整內容(起始、內容及結束)寫入 XML 文件或串流,並指定為 XmlWriter

WriteObjectContent(XmlDictionaryWriter, Object)

僅將物件內容寫入 XML 文件或串流,使用指定的 XmlDictionaryWriter

WriteObjectContent(XmlWriter, Object)

僅將物件內容寫入指定的 XmlWriterXML 文件或串流。

WriteStartObject(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter

WriteStartObject(XmlWriter, Object)

使用指定的 XmlWriter

適用於

另請參閱