Freigeben über


XAttribute Constructor (XName, Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the XAttribute class from the specified name and value.

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

Syntax

'Declaration
Public Sub New ( _
    name As XName, _
    value As Object _
)
public XAttribute(
    XName name,
    Object value
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

The name or value parameter is nulla null reference (Nothing in Visual Basic).

Remarks

There is an implicit conversion from string to XName. Typical use of this constructor is to specify a string as the first parameter instead of creating a new XName, as follows:

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

You can also use the addition operator overload with an XNamespace and a string to create an XName, as follows:

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

For more information, see Working With Namespaces.

These same approaches will work for Visual Basic, however XML literals provide a better approach for creating XML trees.

The value parameter can be a String, double, float, decimal, bool, DateTime, or TimeSpan. If the value is a DateTime or TimeSpan, the value of the attribute is formatted correctly per the W3C specifications.

Examples

The following example uses this constructor to create attributes. It passes strings as the first argument to the XAttribute constructor, which are then implicitly converted to XName objects. The attributes are added to an element.

Dim output As New StringBuilder
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 %>
                       />
output.Append(root)
output.Append(Environment.NewLine)

OutputTextBlock.Text = output.ToString()
    StringBuilder output = new StringBuilder();
    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
    );

    output.Append(root + Environment.NewLine);

    OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.