XmlObjectSerializer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供用來將物件序列化為 XML 資料流或文件的基底類別。 這個類別是抽象的。
public ref class XmlObjectSerializer abstract
public abstract class XmlObjectSerializer
type XmlObjectSerializer = class
Public MustInherit Class XmlObjectSerializer
- 繼承
-
XmlObjectSerializer
- 衍生
範例
下列範例會示範名為 WriteObjectWithInstance
的方法,這個方法會包含 XmlObjectSerializer 做為參數。 這個方法會呼叫 DataContractSerializer 方法,藉以序列化使用 NetDataContractSerializer 或 WriteObject 的物件。
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,而且會用來序列化和還原序列化符合資料合約規則的物件 (使用 DataContractAttribute 和 DataMemberAttribute 建立的物件)。
給實施者的注意事項
當您繼承自 XmlObjectSerializer 時,您必須覆寫下列成員:WriteStartObject(XmlDictionaryWriter, Object)、WriteObjectContent(XmlDictionaryWriter, Object),WriteEndObject(XmlDictionaryWriter)。 此外,您必須實作 IsStartObject 和 ReadObject 方法來進行讀取和還原序列化。
建構函式
XmlObjectSerializer() |
初始化 XmlObjectSerializer 類別的新執行個體。 |