XmlObjectSerializer XmlObjectSerializer XmlObjectSerializer XmlObjectSerializer Class

定義

提供用來將物件序列化為 XML 資料流或文件的基底類別。Provides the base class used to serialize objects as XML streams or documents. 這個類別是抽象的。This class is abstract.

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

範例

下列範例會示範名為 WriteObjectWithInstance 的方法,這個方法會包含 XmlObjectSerializer 做為參數。The following example shows a method named WriteObjectWithInstance that includes an XmlObjectSerializer as a parameter. 這個方法會呼叫 DataContractSerializer 方法,藉以序列化使用 NetDataContractSerializerWriteObject 的物件。The method serializes an object using either the DataContractSerializer or NetDataContractSerializer by calling the WriteObject method.

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,便可建立您自己的序列化程式來序列化或還原序列化物件。Extend the XmlObjectSerializer to create your own serializer to serialize and deserialize objects. DataContractSerializer 類別和 NetDataContractSerializer 類別都是繼承自 XmlObjectSerializer,而且會用來序列化和還原序列化符合資料合約規則的物件 (使用 DataContractAttributeDataMemberAttribute 建立的物件)。Both the DataContractSerializer class and the NetDataContractSerializer class inherit from the XmlObjectSerializer and are used to serialize and deserialize objects that conform to data contract rules (objects created using the DataContractAttribute and the DataMemberAttribute).

給繼承者的注意事項

當您繼承自 XmlObjectSerializer 時,您必須覆寫下列成員:WriteStartObject(XmlDictionaryWriter, Object)WriteObjectContent(XmlDictionaryWriter, Object)WriteEndObject(XmlDictionaryWriter)When you inherit from XmlObjectSerializer, you must override the following members: WriteStartObject(XmlDictionaryWriter, Object), WriteObjectContent(XmlDictionaryWriter, Object), WriteEndObject(XmlDictionaryWriter). 此外,您必須實作 IsStartObjectReadObject 方法來進行讀取和還原序列化。Additionally, the IsStartObject and ReadObject methods must be implemented for reading and deserializing.

建構函式

XmlObjectSerializer() XmlObjectSerializer() XmlObjectSerializer() XmlObjectSerializer()

初始化 XmlObjectSerializer 類別的新執行個體。Initializes a new instance of the XmlObjectSerializer class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsStartObject(XmlDictionaryReader) IsStartObject(XmlDictionaryReader) IsStartObject(XmlDictionaryReader) IsStartObject(XmlDictionaryReader)

取得值,這個值會指定 XmlDictionaryReader 是否位於能夠讀取的 XML 項目上。Gets a value that specifies whether the XmlDictionaryReader is positioned over an XML element that can be read.

IsStartObject(XmlReader) IsStartObject(XmlReader) IsStartObject(XmlReader) IsStartObject(XmlReader)

取得值,這個值會指定 XmlReader 是否位於能夠讀取的 XML 項目上。Gets a value that specifies whether the XmlReader is positioned over an XML element that can be read.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ReadObject(Stream) ReadObject(Stream) ReadObject(Stream) ReadObject(Stream)

使用 Stream 讀取 XML 資料流或文件,然後傳回已還原序列化的物件。Reads the XML stream or document with a Stream and returns the deserialized object.

ReadObject(XmlDictionaryReader) ReadObject(XmlDictionaryReader) ReadObject(XmlDictionaryReader) ReadObject(XmlDictionaryReader)

使用 XmlDictionaryReader 讀取 XML 文件或資料流,然後傳回已還原序列化的物件。Reads the XML document or stream with an XmlDictionaryReader and returns the deserialized object.

ReadObject(XmlDictionaryReader, Boolean) ReadObject(XmlDictionaryReader, Boolean) ReadObject(XmlDictionaryReader, Boolean) ReadObject(XmlDictionaryReader, Boolean)

使用 XmlDictionaryReader 讀取 XML 資料流或文件,然後傳回已還原序列化的物件;它也可以讓您指定序列化程式在嘗試讀取資料前是否能夠讀取資料。Reads the XML stream or document with an XmlDictionaryReader and returns the deserialized object; it also enables you to specify whether the serializer can read the data before attempting to read it.

ReadObject(XmlReader) ReadObject(XmlReader) ReadObject(XmlReader) ReadObject(XmlReader)

使用 XmlReader 讀取 XML 文件或資料流,然後傳回已還原序列化的物件。Reads the XML document or stream with an XmlReader and returns the deserialized object.

ReadObject(XmlReader, Boolean) ReadObject(XmlReader, Boolean) ReadObject(XmlReader, Boolean) ReadObject(XmlReader, Boolean)

使用 XmlReader 讀取 XML 文件或資料流,然後傳回已還原序列化的物件;它也可以讓您指定序列化程式在嘗試讀取資料前是否能夠讀取資料。Reads the XML document or stream with an XmlReader and returns the deserialized object; it also enables you to specify whether the serializer can read the data before attempting to read it.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
WriteEndObject(XmlDictionaryWriter) WriteEndObject(XmlDictionaryWriter) WriteEndObject(XmlDictionaryWriter) WriteEndObject(XmlDictionaryWriter)

使用 XmlDictionaryWriter,將物件資料的結尾當做結尾 XML 項目寫入至 XML 文件或資料流。Writes the end of the object data as a closing XML element to the XML document or stream with an XmlDictionaryWriter.

WriteEndObject(XmlWriter) WriteEndObject(XmlWriter) WriteEndObject(XmlWriter) WriteEndObject(XmlWriter)

使用 XmlWriter,將物件資料的結尾當做結尾 XML 項目寫入至 XML 文件或資料流。Writes the end of the object data as a closing XML element to the XML document or stream with an XmlWriter.

WriteObject(Stream, Object) WriteObject(Stream, Object) WriteObject(Stream, Object) WriteObject(Stream, Object)

使用指定的 Stream,將物件的完整內容 (開始、內容和結束) 寫入至 XML 文件或資料流。Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified Stream.

WriteObject(XmlDictionaryWriter, Object) WriteObject(XmlDictionaryWriter, Object) WriteObject(XmlDictionaryWriter, Object) WriteObject(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter,將物件的完整內容 (開始、內容和結束) 寫入至 XML 文件或資料流。Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified XmlDictionaryWriter.

WriteObject(XmlWriter, Object) WriteObject(XmlWriter, Object) WriteObject(XmlWriter, Object) WriteObject(XmlWriter, Object)

使用指定的 XmlWriter,將物件的完整內容 (開始、內容和結束) 寫入至 XML 文件或資料流。Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified XmlWriter.

WriteObjectContent(XmlDictionaryWriter, Object) WriteObjectContent(XmlDictionaryWriter, Object) WriteObjectContent(XmlDictionaryWriter, Object) WriteObjectContent(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter,只將物件內容寫入至 XML 文件或資料流。Writes only the content of the object to the XML document or stream using the specified XmlDictionaryWriter.

WriteObjectContent(XmlWriter, Object) WriteObjectContent(XmlWriter, Object) WriteObjectContent(XmlWriter, Object) WriteObjectContent(XmlWriter, Object)

使用指定的 XmlWriter,只將物件內容寫入至 XML 文件或資料流。Writes only the content of the object to the XML document or stream with the specified XmlWriter.

WriteStartObject(XmlDictionaryWriter, Object) WriteStartObject(XmlDictionaryWriter, Object) WriteStartObject(XmlDictionaryWriter, Object) WriteStartObject(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter,將物件資料的開始內容當做開頭 XML 項目寫入。Writes the start of the object's data as an opening XML element using the specified XmlDictionaryWriter.

WriteStartObject(XmlWriter, Object) WriteStartObject(XmlWriter, Object) WriteStartObject(XmlWriter, Object) WriteStartObject(XmlWriter, Object)

使用指定的 XmlWriter,將物件資料的開始內容當做開頭 XML 項目寫入。Writes the start of the object's data as an opening XML element using the specified XmlWriter.

適用於

另請參閱