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 类的新实例。 |