通过


XmlAttributes 类

定义

表示控制如何序列化和反序列化对象的属性对象的 XmlSerializer 集合。

public ref class XmlAttributes
public class XmlAttributes
type XmlAttributes = class
Public Class XmlAttributes
继承
XmlAttributes

示例

下面的示例序列化名为Orchestra的类的实例,该实例包含一个名为返回对象数组的Instrument单个字段Instruments。 名为 Brass 第二个类的继承自该 Instrument 类。 该示例创建一个 XmlAttributes 对象来重写 Instrument 字段-允许该字段接受 Brass 对象,并将对象添加到 XmlAttributes 类的 XmlAttributeOverrides 实例。

using System;
using System.IO;
using System.Xml.Serialization;

public class Orchestra
{
   public Instrument[] Instruments;
}

public class Instrument
{
   public string Name;
}

public class Brass:Instrument
{
   public bool IsValved;
}

public class Run
{
    public static void Main()
    {
       Run test = new Run();
       test.SerializeObject("Override.xml");
       test.DeserializeObject("Override.xml");
    }

    public void SerializeObject(string filename)
    {
      /* Each overridden field, property, or type requires
      an XmlAttributes object. */
      XmlAttributes attrs = new XmlAttributes();

      /* Create an XmlElementAttribute to override the
      field that returns Instrument objects. The overridden field
      returns Brass objects instead. */
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the element to the collection of elements.
      attrs.XmlElements.Add(attr);

      // Create the XmlAttributeOverrides object.
      XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();

      /* Add the type of the class that contains the overridden
      member and the XmlAttributes to override it with to the
      XmlAttributeOverrides object. */
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Create the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create the object that will be serialized.
      Orchestra band = new Orchestra();

      // Create an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;

      // Serialize the object.
      s.Serialize(writer,band);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlAttributeOverrides attrOverrides =
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Create an XmlElementAttribute to override the Instrument.
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the element to the collection of elements.
      attrs.XmlElements.Add(attr);

      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Create the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      FileStream fs = new FileStream(filename, FileMode.Open);
      Orchestra band = (Orchestra) s.Deserialize(fs);
      Console.WriteLine("Brass:");

      /* The difference between deserializing the overridden
      XML document and serializing it is this: To read the derived
      object values, you must declare an object of the derived type
      (Brass), and cast the Instrument instance to it. */
      Brass b;
      foreach(Instrument i in band.Instruments)
      {
         b = (Brass)i;
         Console.WriteLine(
         b.Name + "\n" +
         b.IsValved);
      }
   }
}
Imports System.IO
Imports System.Xml.Serialization

Public Class Orchestra
    Public Instruments() As Instrument
End Class

Public Class Instrument
    Public Name As String
End Class

Public Class Brass
    Inherits Instrument
    Public IsValved As Boolean
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Override.xml")
        test.DeserializeObject("Override.xml")
    End Sub    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Each overridden field, property, or type requires
        ' an XmlAttributes object. 
        Dim attrs As New XmlAttributes()
        
        ' Create an XmlElementAttribute to override the
        ' field that returns Instrument objects. The overridden field
        ' returns Brass objects instead. 
        Dim attr As New XmlElementAttribute()
        attr.ElementName = "Brass"
        attr.Type = GetType(Brass)
        
        ' Add the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        
        ' Create the XmlAttributeOverrides object.
        Dim attrOverrides As New XmlAttributeOverrides()
        
        ' Add the type of the class that contains the overridden
        ' member and the XmlAttributes to override it with to the
        ' XmlAttributeOverrides object. 
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create the object that will be serialized.
        Dim band As New Orchestra()
        
        ' Create an object of the derived type.
        Dim i As New Brass()
        i.Name = "Trumpet"
        i.IsValved = True
        Dim myInstruments() As Instrument = {i}
        band.Instruments = myInstruments
        
        ' Serialize the object.
        s.Serialize(writer, band)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim attrOverrides As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()
        
        ' Create an XmlElementAttribute to override the Instrument.
        Dim attr As New XmlElementAttribute()
        attr.ElementName = "Brass"
        attr.Type = GetType(Brass)
        
        ' Add the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim band As Orchestra = CType(s.Deserialize(fs), Orchestra)
        Console.WriteLine("Brass:")
        
        ' The difference between deserializing the overridden
        ' XML document and serializing it is this: To read the derived
        ' object values, you must declare an object of the derived type
        ' (Brass), and cast the Instrument instance to it. 
        Dim b As Brass
        Dim i As Instrument
        For Each i In  band.Instruments
            b = CType(i, Brass)
            Console.WriteLine(b.Name + ControlChars.Cr + _
                              b.IsValved.ToString())
        Next i
    End Sub
End Class

注解

XmlAttributes创建过程是重写序列化类实例的默认方式XmlSerializer的过程的一部分。 例如,假设你想要序列化从具有不可访问源的 DLL 创建的对象。 通过使用 XmlAttributeOverrides,可以扩充或控制对象的序列化方式。

类的成员 XmlAttributes 直接与控制序列化的属性类系列相对应。 例如, XmlText 属性必须设置为一个 XmlTextAttribute属性,这样就可以通过指示 XmlSerializer 将属性值序列化为 XML 文本来替代字段或属性的序列化。 有关控制序列化的属性的完整列表,请参阅 XmlSerializer.

有关将 XmlAttributeOverrides 类与类一起使用 XmlAttributes 的更多详细信息,请参阅 How to: Specify an Alternate Element Name for an XML Stream.

构造函数

名称 说明
XmlAttributes()

初始化 XmlAttributes 类的新实例。

XmlAttributes(ICustomAttributeProvider)

初始化类的新实例 XmlAttributes 并自定义序列化和反序列化对象的方式 XmlSerializer

属性

名称 说明
XmlAnyAttribute

获取或设置 XmlAnyAttributeAttribute 要重写的项。

XmlAnyElements

获取要重写的对象 XmlAnyElementAttribute 集合。

XmlArray

获取或设置一个对象,该对象指定如何 XmlSerializer 序列化返回数组的公共字段或读/写属性。

XmlArrayItems

获取或设置一个对象集合,这些对象指定如何 XmlSerializer 序列化插入到公共字段或读/写属性返回的数组中的项。

XmlAttribute

获取或设置一个对象,该对象指定如何将 XmlSerializer 公共字段或公共读/写属性序列化为 XML 属性。

XmlChoiceIdentifier

获取或设置一个对象,该对象允许区分一组选项。

XmlDefaultValue

获取或设置 XML 元素或属性的默认值。

XmlElements

获取一个对象集合,这些对象指定如何将 XmlSerializer 公共字段或读/写属性序列化为 XML 元素。

XmlEnum

获取或设置一个对象,该对象指定 XmlSerializer 如何序列化枚举成员。

XmlIgnore

获取或设置一个值,该值指定是 XmlSerializer 序列化公共字段还是公共读/写属性。

Xmlns

获取或设置一个值,该值指定在包含返回对象的成员 XmlSerializerNamespaces 的对象被重写时是否保留所有命名空间声明。

XmlRoot

获取或设置一个对象,该对象指定如何将 XmlSerializer 类序列化为 XML 根元素。

XmlText

获取或设置一个对象,该 XmlSerializer 对象指示将公共字段或公共读/写属性序列化为 XML 文本。

XmlType

获取或设置一个对象,该对象指定如何 XmlSerializer 序列化应用该类的 XmlTypeAttribute 类。

方法

名称 说明
Equals(Object)

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

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

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

(继承自 Object)

适用于

另请参阅