共用方式為


XmlObjectSerializer 類別

定義

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

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

範例

下列範例會示範名為 WriteObjectWithInstance 的方法,這個方法會包含 XmlObjectSerializer 做為參數。 這個方法會呼叫 DataContractSerializer 方法,藉以序列化使用 NetDataContractSerializerWriteObject 的物件。

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)。 此外,您必須實作 IsStartObjectReadObject 方法來進行讀取和還原序列化。

建構函式

XmlObjectSerializer()

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

方法

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)

使用 XmlDictionaryReader 讀取 XML 文件或資料流,然後傳回已還原序列化的物件。

ReadObject(XmlDictionaryReader, Boolean)

使用 XmlDictionaryReader 讀取 XML 資料流或文件,然後傳回已還原序列化的物件;它也可以讓您指定序列化程式在嘗試讀取資料前是否能夠讀取資料。

ReadObject(XmlReader)

使用 XmlReader 讀取 XML 文件或資料流,然後傳回已還原序列化的物件。

ReadObject(XmlReader, Boolean)

使用 XmlReader 讀取 XML 文件或資料流,然後傳回已還原序列化的物件;它也可以讓您指定序列化程式在嘗試讀取資料前是否能夠讀取資料。

ToString()

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

(繼承來源 Object)
WriteEndObject(XmlDictionaryWriter)

使用 XmlDictionaryWriter,將物件資料的結尾當做結尾 XML 項目寫入至 XML 文件或資料流。

WriteEndObject(XmlWriter)

使用 XmlWriter,將物件資料的結尾當做結尾 XML 項目寫入至 XML 文件或資料流。

WriteObject(Stream, Object)

使用指定的 Stream,將物件的完整內容 (開始、內容和結束) 寫入至 XML 文件或資料流。

WriteObject(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter,將物件的完整內容 (開始、內容和結束) 寫入至 XML 文件或資料流。

WriteObject(XmlWriter, Object)

使用指定的 XmlWriter,將物件的完整內容 (開始、內容和結束) 寫入至 XML 文件或資料流。

WriteObjectContent(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter,只將物件內容寫入至 XML 文件或資料流。

WriteObjectContent(XmlWriter, Object)

使用指定的 XmlWriter,只將物件內容寫入至 XML 文件或資料流。

WriteStartObject(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter,將物件資料的開始內容當做開頭 XML 項目寫入。

WriteStartObject(XmlWriter, Object)

使用指定的 XmlWriter,將物件資料的開始內容當做開頭 XML 項目寫入。

適用於

另請參閱