通过


XmlAttributeAttribute 类

定义

指定 XmlSerializer 必须将类成员序列化为 XML 属性。

public ref class XmlAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlAttributeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlAttributeAttribute = class
    inherit Attribute
Public Class XmlAttributeAttribute
Inherits Attribute
继承
XmlAttributeAttribute
属性

示例

以下示例序列化一个类,该类包含应用该字段的 XmlAttributeAttribute 多个字段。

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

public class Group
{
   [XmlAttribute (Namespace = "http://www.cpandl.com")]
   public string GroupName;

   [XmlAttribute(DataType = "base64Binary")]
   public Byte [] GroupNumber;

   [XmlAttribute(DataType = "date", AttributeName = "CreationDate")]
   public DateTime Today;
}

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

   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Group));

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

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";

      Byte [] hexByte = new Byte[2]{Convert.ToByte(100),
      Convert.ToByte(50)};
      myGroup.GroupNumber = hexByte;

      DateTime myDate = new DateTime(2001,1,10);
      myGroup.Today = myDate;

      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
       writer.Close();
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Schema


Public Class Group
    <XmlAttribute(Namespace := "http://www.cpandl.com")> _
        Public GroupName As String    
    <XmlAttribute(DataType := "base64Binary")> _
        Public GroupNumber() As Byte    
    <XmlAttribute(DataType := "date", AttributeName := "CreationDate")> _
        Public Today As DateTime
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Attributes.xml")
    End Sub 
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As New XmlSerializer(GetType(Group))
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the class that will be serialized.
        Dim myGroup As New Group()
        
        ' Set the object properties.
        myGroup.GroupName = ".NET"
        
        Dim hexByte() As Byte = {Convert.ToByte(100), Convert.ToByte(50)}
        myGroup.GroupNumber = hexByte
        
        Dim myDate As New DateTime(2001, 1, 10)
        myGroup.Today = myDate
        
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class

注解

属于 XmlAttributeAttribute 一系列属性,用于控制序列化或反序列化对象的方式 XmlSerializer 。 有关类似属性的完整列表,请参阅 控制 XML 序列化的属性。

应用于公共字段或属性时,通知XmlAttributeAttributeXmlSerializer将成员序列化为 XML 属性。 默认情况下,将 XmlSerializer 公共字段和属性序列化为 XML 元素。

只能向返回值(或值数组)的公共字段或公共属性分配 XmlAttributeAttribute ,这些值可以映射到 XML 架构定义语言(XSD)简单类型之一(包括派生自 XSD anySimpleType 类型的所有内置数据类型)。 可能的类型包括可映射到 XSD 简单类型(包括 GuidChar和枚举的任何类型。 DataType请参阅 XSD 类型的列表的属性,以及如何 to.NET 数据类型映射它们。

可以使用以下两个特殊属性进行设置 XmlAttributeAttributexml:lang (指定语言)和 xml:space (指定如何处理空格)属性。 这些属性旨在传达仅与处理 XML 的应用程序相关的信息。 以下代码显示了这些设置的示例。

[XmlAttribute("xml:lang")]
 public string Lang;
 // Set this to 'default' or 'preserve'.
 [XmlAttribute("space",
 Namespace = "http://www.w3.org/XML/1998/namespace")]
 public string Space
<XmlAttribute("xml:lang")> _
Public Lang As String
' Set this to 'default' or 'preserve'.
<XmlAttribute("space", _
Namespace:= "http://www.w3.org/XML/1998/namespace")> _
Public Space As String

有关使用属性的详细信息,请参阅 “属性”。

注释

可以在代码中使用单词 XmlAttribute ,而不是较长 XmlAttributeAttribute时间。

构造函数

名称 说明
XmlAttributeAttribute()

初始化 XmlAttributeAttribute 类的新实例。

XmlAttributeAttribute(String, Type)

初始化 XmlAttributeAttribute 类的新实例。

XmlAttributeAttribute(String)

初始化类的新实例 XmlAttributeAttribute 并指定生成的 XML 属性的名称。

XmlAttributeAttribute(Type)

初始化 XmlAttributeAttribute 类的新实例。

属性

名称 说明
AttributeName

获取或设置 XML 属性的名称。

DataType

获取或设置由 XmlSerializer该属性生成的 XML 属性的 XSD 数据类型。

Form

获取或设置一个值,该值指示是否限定由 XmlSerializer XML 属性名称生成。

Namespace

获取或设置 XML 属性的 XML 命名空间。

Type

获取或设置 XML 属性的复杂类型。

TypeId

在派生类中实现时,获取此 Attribute的唯一标识符。

(继承自 Attribute)

方法

名称 说明
Equals(Object)

返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否为派生类的默认值。

(继承自 Attribute)
Match(Object)

在派生类中重写时,返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

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

(继承自 Object)

显式接口实现

名称 说明
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,该信息可用于获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对对象公开的属性和方法的访问。

(继承自 Attribute)

适用于