다음을 통해 공유


XAttribute Class

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

Represents an XML attribute.

Inheritance Hierarchy

System.Object
  System.Xml.Linq.XObject
    System.Xml.Linq.XAttribute

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

Syntax

'Declaration
Public Class XAttribute _
    Inherits XObject
public class XAttribute : XObject

The XAttribute type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XAttribute(XAttribute) Initializes a new instance of the XAttribute class from another XAttribute object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XAttribute(XName, Object) Initializes a new instance of the XAttribute class from the specified name and value.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 BaseUri Gets the base URI for this XObject. (Inherited from XObject.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Document Gets the XDocument for this XObject. (Inherited from XObject.)
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 EmptySequence Gets an empty collection of attributes.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsNamespaceDeclaration Determines if this attribute is a namespace declaration.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Name Gets the expanded name of this attribute.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NextAttribute Gets the next attribute of the parent element.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NodeType Gets the node type for this node. (Overrides XObject.NodeType.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Parent Gets the parent XElement of this XObject. (Inherited from XObject.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PreviousAttribute Gets the previous attribute of the parent element.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Value Gets or sets the value of this attribute.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 AddAnnotation Adds an object to the annotation list of this XObject. (Inherited from XObject.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Annotation(Type) Gets the first annotation object of the specified type from this XObject. (Inherited from XObject.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Annotation<T>() Get the first annotation object of the specified type from this XObject. (Inherited from XObject.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Annotations(Type) Gets a collection of annotations of the specified type for this XObject. (Inherited from XObject.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Annotations<T>() Gets a collection of annotations of the specified type for this XObject. (Inherited from XObject.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Remove Removes this attribute from its parent element.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 RemoveAnnotations(Type) Removes the annotations of the specified type from this XObject. (Inherited from XObject.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 RemoveAnnotations<T>() Removes the annotations of the specified type from this XObject. (Inherited from XObject.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 SetValue Sets the value of this attribute.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Converts the current XAttribute object to a string representation. (Overrides Object.ToString().)

Top

Events

  Name Description
Public eventSupported by Silverlight for Windows PhoneSupported by Xbox 360 Changed Raised when this XObject or any of its descendants have changed. (Inherited from XObject.)
Public eventSupported by Silverlight for Windows PhoneSupported by Xbox 360 Changing Raised when this XObject or any of its descendants are about to change. (Inherited from XObject.)

Top

Operators

  Name Description
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to DateTime) Cast the value of this XAttribute to a DateTime.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<DateTime>) Cast the value of this XAttribute to a Nullable<T> of DateTime.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<Decimal>) Cast the value of this XAttribute to a Nullable<T> of Decimal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<Double>) Cast the value of this XAttribute to a Nullable<T> of Double.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Decimal) Cast the value of this XAttribute to a Decimal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to DateTimeOffset) Cast the value of this XAttribute to a DateTimeOffset.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Guid) Cast the value of this XAttribute to a Guid.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<Guid>) Cast the value of this XAttribute to a Nullable<T> of Guid.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<TimeSpan>) Cast the value of this XAttribute to a Nullable<T> of TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<DateTimeOffset>) Cast the value of this XAttribute to a Nullable<T> of DateTimeOffset.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to TimeSpan) Cast the value of this XAttribute to a TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Double) Cast the value of this XAttribute to a Double.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Int32) Cast the value of this XAttribute to an Int32.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<Int32>) Cast the value of this XAttribute to a Nullable<T> of Int32.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to UInt32) Cast the value of this XAttribute to a UInt32.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to String) Cast the value of this XAttribute to a String.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Boolean) Cast the value of this XAttribute to a Boolean.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<Boolean>) Cast the value of this XAttribute to a Nullable<T> of Boolean.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<UInt32>) Cast the value of this XAttribute to a Nullable<T> of UInt32.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<UInt64>) Cast the value of this XAttribute to a Nullable<T> of UInt64.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Single) Cast the value of this XAttribute to a Single.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<Single>) Cast the value of this XAttribute to a Nullable<T> of Single.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Int64) Cast the value of this XAttribute to an Int64.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to Nullable<Int64>) Cast the value of this XAttribute to a Nullable<T> of Int64.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Explicit(XAttribute to UInt64) Cast the value of this XAttribute to a UInt64.

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IXmlLineInfo.HasLineInfo Gets a value indicating whether or not this XObject has line information. (Inherited from XObject.)
Explicit interface implemetationPrivate propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IXmlLineInfo.LineNumber Gets the line number that the underlying XmlReader reported for this XObject. (Inherited from XObject.)
Explicit interface implemetationPrivate propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IXmlLineInfo.LinePosition Gets the line position that the underlying XmlReader reported for this XObject. (Inherited from XObject.)

Top

Remarks

An XML attribute is a name/value pair associated with an XML element.

Each XElement contains a list of the attributes for that element. Attributes must have a qualified name that is unique to the element. Attributes are not derived from XNode; they are not nodes in the XML tree. Instead, they are simply name/value pairs associated with an element.

Attributes are maintained in the XML tree in the order that they were added to the element. When a collection of attributes is returned by Attributes, they are returned in the order that they were added to the element, and are not sorted.

Technically, in XML, namespace declarations are not attributes proper. However, this distinction is not normally made by many XML programmers. Instead, because namespace declarations have exactly the same XML syntax as attributes, most XML programmers think of namespaces as attributes. To simplify the LINQ to XML programming interface, namespaces are represented in the XML tree as attributes. Such namespace attributes impact serialization of an XML tree. When serializing, LINQ to XML attempts to serialize with the namespace prefix specified in namespace attributes. You can use the IsNamespaceDeclaration to determine if an attribute is really a namespace declaration.

The XAttribute class derives from XObject. You can add annotations to attributes, and observe events for them.

Some XAttribute methods can be used from XAML, for more information, see LINQ to XML Dynamic Properties in the .NET Framework documentation.

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.