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 的浅表副本。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.

适用于

另请参阅