XAttribute.Value Właściwość

Definicja

Pobiera lub ustawia wartość tego atrybutu.

C#
public string Value { get; set; }

Wartość właściwości

Element String zawierający wartość tego atrybutu.

Wyjątki

W przypadku ustawienia parametr ma value wartość null.

Przykłady

Poniższy przykład tworzy element z atrybutem . Następnie pobiera wartość atrybutu, a następnie ustawia ją.

Zwróć uwagę, że w przykładzie języka Visual Basic użyto właściwości atrybutu XML.

C#
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);  

Ten przykład generuje następujące wyniki:

content  
new text  

W poniższym przykładzie pokazano korzyść użycia jawnych operatorów konwersji w celu uzyskania wartości atrybutu, który może nie istnieć:

C#
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());  

Ten przykład generuje następujące wyniki:

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  

Uwagi

Tej właściwości można użyć do pobrania lub ustawienia wartości atrybutu.

Ustawienie tej właściwości spowoduje wywołanie Changed zdarzeń i Changing .

Jeśli otrzymujesz wartość i atrybut może nie istnieć, jest to wygodniejsze użycie jawnych operatorów konwersji i przypisanie atrybutu do typu dopuszczającego wartość null, takiego jak string lub Int32Nullable<T> . Jeśli atrybut nie istnieje, typ dopuszczający wartość null jest ustawiony na nullwartość . Przed użyciem tej właściwości upewnij się, że Attribute metoda nie zwraca nullwartości .

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Zobacz też