次の方法で共有


XAttribute.Value Property

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

Gets or sets the value of this attribute.

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

Syntax

'Declaration
Public Property Value As String
public string Value { get; set; }

Property Value

Type: System.String
A String containing the value of this attribute.

Exceptions

Exception Condition
ArgumentNullException

When setting, the value is nulla null reference (Nothing in Visual Basic).

Remarks

You can use this property to get or set the value of an attribute.

Setting this property will raise the Changed and the Changing events.

If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as string or Nullable<T> of Int32. If the attribute does not exist, then the nullable type is set to nulla null reference (Nothing in Visual Basic). Before using this property, you must make sure that the Attribute method does not return nulla null reference (Nothing in Visual Basic).

Examples

The following example creates an element with an attribute. It then retrieves the value of the attribute, and then sets it.

Note that the Visual Basic example uses the XML attribute property.

Dim output As New StringBuilder
Dim root As XElement = <Root Att="content"/>
output.Append(root.@Att)
output.Append(Environment.NewLine)
root.@Att = "new text"
output.Append(root.@Att)
output.Append(Environment.NewLine)


OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
XElement root = new XElement("Root",
    new XAttribute("Att", "content")
);
XAttribute att = root.FirstAttribute;
output.Append(att.Value + Environment.NewLine);
att.Value = "new text";
output.Append(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.