XAttribute.Value Property
Gets or sets the value of this attribute.
Namespace: System.Xml.Linq
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
| Exception | Condition |
|---|---|
| ArgumentNullException | When setting, the value is null. |
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 null. Before using this property, you must make sure that the Attribute method does not return null.
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.
XElement root = new XElement("Root", new XAttribute("Att", "content") ); XAttribute att = root.FirstAttribute; Console.WriteLine(att.Value); att.Value = "new text"; Console.WriteLine(att.Value);
This example produces the following output:
content new text
The following example shows the benefit of using the explicit conversion operators to get the value of an attribute that might not exist:
XElement root = new XElement("Root", new XAttribute("Att1", "attribute 1 content"), new XAttribute("Att2", "2") ); // The following assignments demonstrate why it is easier to use // casting when the attribute might or might not exist. string c1 = (string)root.Attribute("Att1"); Console.WriteLine("c1:{0}", c1 == null ? "attribute does not exist" : c1); int? c2 = (int?)root.Attribute("Att2"); Console.WriteLine("c2:{0}", c2 == null ? "attribute does not exist" : c2.ToString()); string c3 = (string)root.Attribute("Att3"); Console.WriteLine("c3:{0}", c3 == null ? "attribute does not exist" : c3); int? c4 = (int?)root.Attribute("Att4"); Console.WriteLine("c4:{0}", c4 == null ? "attribute does not exist" : c4.ToString()); Console.WriteLine(); // The following assignments show the necessary code when using // the value property when the attribute might or might not exist. XAttribute att1 = root.Attribute("Att1"); string v1; if (att1 == null) v1 = null; else v1 = att1.Value; Console.WriteLine("v1:{0}", v1 == null ? "attribute does not exist" : v1); XAttribute att2 = root.Attribute("Att2"); int? v2; if (att2 == null) v2 = null; else v2 = Int32.Parse(att2.Value); Console.WriteLine("v2:{0}", v2 == null ? "attribute does not exist" : v2.ToString()); XAttribute att3 = root.Attribute("Att3"); string v3; if (att3 == null) v3 = null; else v3 = att3.Value; Console.WriteLine("v3:{0}", v3 == null ? "attribute does not exist" : v3); XAttribute att4 = root.Attribute("Att4"); int? v4; if (att4 == null) v4 = null; else v4 = Int32.Parse(att4.Value); Console.WriteLine("v4:{0}", v4 == null ? "attribute does not exist" : v4.ToString());
This example produces the following output:
c1:attribute 1 content c2:2 c3:attribute does not exist c4:attribute does not exist v1:attribute 1 content v2:2 v3:attribute does not exist v4:attribute does not exist
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.