XmlObjectSerializer 类

定义

提供用于将对象序列化为 XML 流或文档的基类。 此类是抽象的。

public ref class XmlObjectSerializer abstract
public abstract class XmlObjectSerializer
type XmlObjectSerializer = class
Public MustInherit Class XmlObjectSerializer
继承
XmlObjectSerializer
派生

示例

下面的示例演示了一个名为 WriteObjectWithInstance 包含 XmlObjectSerializer 参数的方法。 该方法使用 DataContractSerializerNetDataContractSerializer 调用 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扩展创建自己的序列化程序以序列化和反序列化对象。 类和类都继承自该类,并用于序列化和反序列化符合数据协定规则的对象(使用和 <a0/> 创建的对象)。

实施者说明

XmlObjectSerializer中继承时,必须重写以下成员:WriteStartObject(XmlDictionaryWriter, Object)、、 WriteObjectContent(XmlDictionaryWriter, Object)WriteEndObject(XmlDictionaryWriter)。 此外, IsStartObject 必须实现用于读取和反序列化的和 ReadObject 方法。

构造函数

名称 说明
XmlObjectSerializer()

初始化 XmlObjectSerializer 类的新实例。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
IsStartObject(XmlDictionaryReader)

获取一个值,该值指定是否 XmlDictionaryReader 放置在可读取的 XML 元素上。

IsStartObject(XmlReader)

获取一个值,该值指定是否 XmlReader 放置在可读取的 XML 元素上。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ReadObject(Stream)

使用反序列化对象读取 XML 流或文档 Stream 并返回该对象。

ReadObject(XmlDictionaryReader, Boolean)

使用 XmlDictionaryReader 并返回反序列化对象读取 XML 流或文档;它还允许指定序列化程序在尝试读取数据之前是否可以读取数据。

ReadObject(XmlDictionaryReader)

读取带有 XmlDictionaryReader 反序列化对象的 XML 文档或流。

ReadObject(XmlReader, Boolean)

使用 XML 文档或流 XmlReader 读取并返回反序列化对象;它还允许指定序列化程序在尝试读取数据之前是否可以读取数据。

ReadObject(XmlReader)

读取带有 XmlReader 反序列化对象的 XML 文档或流。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
WriteEndObject(XmlDictionaryWriter)

将对象数据的末尾作为结束 XML 元素写入 XML 文档或带有 < a0 /> 的流中。

WriteEndObject(XmlWriter)

将对象数据的末尾作为结束 XML 元素写入 XML 文档或带有 < a0 /> 的流中。

WriteObject(Stream, Object)

将对象的完整内容(start、content 和 end)写入到具有指定对象的 StreamXML 文档或流中。

WriteObject(XmlDictionaryWriter, Object)

将对象的完整内容(start、content 和 end)写入到具有指定对象的 XmlDictionaryWriterXML 文档或流中。

WriteObject(XmlWriter, Object)

将对象的完整内容(start、content 和 end)写入到具有指定对象的 XmlWriterXML 文档或流中。

WriteObjectContent(XmlDictionaryWriter, Object)

仅使用指定的 XmlDictionaryWriter对象将对象的内容写入 XML 文档或流。

WriteObjectContent(XmlWriter, Object)

仅将对象的内容写入具有指定 XmlWriter对象的 XML 文档或流。

WriteStartObject(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriterXML 元素将对象的数据的开头写入打开的 XML 元素。

WriteStartObject(XmlWriter, Object)

使用指定的 XmlWriterXML 元素将对象的数据的开头写入打开的 XML 元素。

适用于

另请参阅