Condividi tramite


XAttribute.Name Property

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

Gets the expanded name of this attribute.

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

Syntax

'Declaration
Public ReadOnly Property Name As XName
public XName Name { get; }

Property Value

Type: System.Xml.Linq.XName
An XName containing the name of this attribute.

Remarks

The expanded name returned by this property is in the form of {namespace}localname.

Examples

The following example creates an element with three attributes. It then uses this property to print out the name of each attribute. The example also shows creation of a new attribute using the name of an existing attribute.

            Dim output As New StringBuilder
            Dim root As XElement = _
                <aw:Root xmlns:aw='https://www.adventure-works.com'
                    aw:Att='content'
                    Att2='different content'/>

            For Each att As XAttribute In root.Attributes()
                output.Append(String.Format("{0}={1}", att.Name, att.Value))
                output.Append(Environment.NewLine)
            Next
            output.Append("")
            output.Append(Environment.NewLine)

            Dim NewRoot As XElement = _
            <Root
                <%= _
                    From att In root.Attributes("Att2") _
                    Select New XAttribute(att.Name, "new content") _
                %>>_
</Root>

            For Each att As XAttribute In NewRoot.Attributes()
                output.Append(String.Format("{0}={1}", att.Name, att.Value))
                output.Append(Environment.NewLine)
            Next


            OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
XNamespace aw = "https://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XAttribute(XNamespace.Xmlns + "aw", "https://www.adventure-works.com"),
    new XAttribute(aw + "Att", "content"),
    new XAttribute("Att2", "different content")
);

foreach (XAttribute att in root.Attributes())
    output.Append(att.Name + "=" + att.Value + Environment.NewLine);
output.Append("" + Environment.NewLine);

XElement newRoot = new XElement(aw + "Root",
    from att in root.Attributes("Att2")
    select new XAttribute(att.Name, "new content"));

foreach (XAttribute att in newRoot.Attributes())
    output.Append(att.Name + "=" + att.Value + 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.