XmlRootAttribute 类

定义

控制视为 XML 根元素的属性目标的 XML 序列化。

C#
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.ReturnValue | System.AttributeTargets.Struct)]
public class XmlRootAttribute : Attribute
继承
XmlRootAttribute
属性

示例

以下示例将 XmlRootAttribute 应用于 类。 特性指定元素名称、命名空间、元素是否限定,以及 xsi:nil 是否在类设置为 null时生成特性。

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

[XmlRoot(Namespace = "www.contoso.com",
     ElementName = "MyGroupName",
     DataType = "string",
     IsNullable=true)]
public class Group
{
    private string groupNameValue;
    // Insert code for the Group class.
    public Group()
    {
    }

    public Group(string groupNameVal)
    {
        groupNameValue = groupNameVal;
    }

    public string GroupName
    {
        get{return groupNameValue;}
        set{groupNameValue = value;}
    }
}
public class Test
{
    static void Main()
    {
        Test t = new Test();
        t.SerializeGroup();
    }

    private void SerializeGroup()
    {
        // Create an instance of the Group class, and an
        // instance of the XmlSerializer to serialize it.
        Group myGroup = new Group("Redmond");
        XmlSerializer ser = new XmlSerializer(typeof(Group));
        // A FileStream is used to write the file.
        FileStream fs = new FileStream("group.xml",FileMode.Create);
        ser.Serialize(fs,myGroup);
        fs.Close();
        Console.WriteLine(myGroup.GroupName);
        Console.WriteLine("Done");
        Console.ReadLine();
    }
}

注解

属于 XmlRootAttribute 一系列属性,这些属性控制如何 XmlSerializer 序列化或反序列化对象。 有关类似属性的完整列表,请参阅 控制 XML 序列化的属性。

可以将 应用于 XmlRootAttribute 类、结构、枚举或接口。 还可以将 特性应用于 XML Web 服务方法的返回值。

每个 XML 文档都必须有一个包含所有其他元素的根元素。 XmlRootAttribute允许通过设置某些属性来控制 生成根元素的方式XmlSerializer。 例如,通过设置 ElementName 属性指定生成的 XML 元素的名称。

有关使用特性的详细信息,请参阅 特性

备注

可以在代码中使用 单词 XmlRoot ,而不是使用较长 XmlRootAttribute的 。

构造函数

XmlRootAttribute()

初始化 XmlRootAttribute 类的新实例。

XmlRootAttribute(String)

初始化 XmlRootAttribute 类的新实例,并指定 XML 根元素的名称。

属性

DataType

获取或设置 XML 根元素的 XSD 数据类型。

ElementName

获取或设置由 XmlSerializer 类的 Serialize(TextWriter, Object)Deserialize(Stream) 方法分别生成和识别的 XML 元素的名称。

IsNullable

获取或设置一个值,该值指示 XmlSerializer 是否必须将设置为 null 的成员序列化为设置为 xsi:niltrue 属性。

Namespace

获取或设置 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)

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

另请参阅