XAttribute 构造函数

定义

初始化 XAttribute 类的新实例。

重载

名称 说明
XAttribute(XAttribute)

从另一个XAttribute对象初始化类的新实例XAttribute

XAttribute(XName, Object)

从指定的名称和值初始化类的新实例 XAttribute

XAttribute(XAttribute)

Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs

从另一个XAttribute对象初始化类的新实例XAttribute

public:
 XAttribute(System::Xml::Linq::XAttribute ^ other);
public XAttribute(System.Xml.Linq.XAttribute other);
new System.Xml.Linq.XAttribute : System.Xml.Linq.XAttribute -> System.Xml.Linq.XAttribute
Public Sub New (other As XAttribute)

参数

other
XAttribute

XAttribute要从中复制的对象。

例外

参数 othernull.

示例

此示例演示如何创建 XML 树的深层副本会创建树中属性的副本,而不是克隆属性。

XElement root1 = XElement.Parse("<Root Att1='abc' />");
// Make a deep copy.
XElement root2 = new XElement(root1);
if (root1.Attribute("Att1") == root2.Attribute("Att1"))
    Console.WriteLine("This will not be printed");
else
    Console.WriteLine("Creating a deep copy created a new attribute from the original.");
Dim root1 As XElement = <Root Att1='abc'/>
' Make a deep copy.
Dim root2 As XElement = New XElement(root1)
If root1.Attribute("Att1") Is root2.Attribute("Att1") Then
    Console.WriteLine("This will not be printed")
Else
    Console.WriteLine("Creating a deep copy created a new attribute from the original.")
End If

此示例生成以下输出:

Creating a deep copy created a new attribute from the original.

注解

在创建 XML 树的深层副本时,此构造函数主要用于内部。

另请参阅

适用于

XAttribute(XName, Object)

Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs

从指定的名称和值初始化类的新实例 XAttribute

public:
 XAttribute(System::Xml::Linq::XName ^ name, System::Object ^ value);
public XAttribute(System.Xml.Linq.XName name, object value);
new System.Xml.Linq.XAttribute : System.Xml.Linq.XName * obj -> System.Xml.Linq.XAttribute
Public Sub New (name As XName, value As Object)

参数

name
XName

XName属性。

value
Object

一个 Object 包含特性的值。

例外

namevalue参数为 null.

示例

以下示例使用此构造函数创建属性。 它将字符串作为第一个参数 XAttribute 传递给构造函数,然后隐式转换为 XName 对象。 属性将添加到元素中。

XElement root;

double dbl = 12.345;
XAttribute[] attArray = {
    new XAttribute("Att4", 1),
    new XAttribute("Att5", 2),
    new XAttribute("Att6", 3)
};
DateTime dt = new DateTime(2006, 10, 6, 12, 30, 00);

// string content
root = new XElement("Root",
    new XAttribute("Att1", "Some text"),

    // double content
    new XAttribute("Att2", dbl),

    // DateTime content
    new XAttribute("Att3", dt),

    // XAttribute array content
    attArray
);

Console.WriteLine(root);
Dim dbl As Double = 12.345
Dim attArray As XAttribute() = { _
    New XAttribute("Att4", 1), _
    New XAttribute("Att5", 2), _
    New XAttribute("Att6", 3) _
}
Dim dt As DateTime = New DateTime(2006, 10, 6, 12, 30, 0)
Dim root As XElement = <Root Att1="Some text"
                           Att2=<%= dbl %>
                           Att3=<%= dt %>
                           <%= attArray %>
                       />
Console.WriteLine(root)

此示例生成以下输出:

<Root Att1="Some text" Att2="12.345" Att3="2006-10-06T12:30:00" Att4="1" Att5="2" Att6="3" />

注解

有从字符串到 XName. 的隐式转换。 此构造函数的典型用法是将字符串指定为第一个参数,而不是创建新的 XName字符串,如下所示:

XElement root = new XElement("Root",
    new XAttribute("AnAttributeName", "Content")
);

还可以使用加法运算符重载和 XNamespace 字符串来创建一个 XName,如下所示:

XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XAttribute(aw + "AnAttributeName", "Content")
);

有关详细信息,请参阅 “使用 XML 命名空间”。

这些相同的方法适用于Visual Basic,但 XML 文本为创建 XML 树提供了更好的方法。

参数value可以是 aString、、doublefloatdecimalbool、 或DateTimeTimeSpan。 如果值为或DateTimeTimeSpan值,则根据 W3C 规范正确设置属性的格式。

另请参阅

适用于