XAttribute.Value 속성

정의

이 특성의 값을 가져오거나 설정합니다.

public:
 property System::String ^ Value { System::String ^ get(); void set(System::String ^ value); };
public string Value { get; set; }
member this.Value : string with get, set
Public Property Value As String

속성 값

이 특성의 값이 들어 있는 String입니다.

예외

설정 시 valuenull인 경우

예제

다음 예제에서는 특성을 사용하여 요소를 만듭니다. 그런 다음 특성의 값을 검색한 다음 설정합니다.

Visual Basic 예제에서는 XML 특성 속성을 사용합니다.

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);  
Dim root As XElement = <Root Att="content"/>  
Console.WriteLine(root.@Att)  
root.@Att = "new text"  
Console.WriteLine(root.@Att)  

이 예제는 다음과 같은 출력을 생성합니다.

content  
new text  

다음 예제에서는 명시적 변환 연산자를 사용하여 존재하지 않을 수 있는 특성의 값을 가져오는 이점을 보여줍니다.

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());  
Dim root As XElement = <Root Att1="attribute 1 content" Att2="2"/>  

' The following assignments demonstrate why it is easier to use  
' casting when the attribute might or might not exist.  

Dim c1 As String = CStr(root.Attribute("Att1"))  
Console.WriteLine("c1:{0}", IIf(c1 Is Nothing, "attribute does not exist", c1))  

Dim c2 As Nullable(Of Integer) = CType(root.Attribute("Att2"), Nullable(Of Integer))  
Console.WriteLine("c2:{0}", IIf(c2.HasValue, c2, "attribute does not exist"))  

Dim c3 As String = CStr(root.Attribute("Att3"))  
Console.WriteLine("c3:{0}", IIf(c3 Is Nothing, "attribute does not exist", c3))  

Dim c4 As Nullable(Of Integer) = CType(root.Attribute("Att4"), Nullable(Of Integer))  
Console.WriteLine("c4:{0}", IIf(c4.HasValue, c4, "attribute does not exist"))  

Console.WriteLine()  

' The following assignments show the necessary code when using  
' the value property when the attribute might or might not exist.  

Dim att1 As XAttribute = root.Attribute("Att1")  
Dim v1 As String  
If att1 Is Nothing Then  
    v1 = Nothing  
Else  
    v1 = att1.Value  
End If  

Console.WriteLine("v1:{0}", IIf(v1 Is Nothing, "attribute does not exist", v1))  

Dim att2 As XAttribute = root.Attribute("Att2")  
Dim v2 As Nullable(Of Integer)  
If att2 Is Nothing Then  
    v2 = Nothing  
Else  
    v2 = Int32.Parse(att2.Value)  
End If  

Console.WriteLine("v2:{0}", IIf(v2.HasValue, v2, "attribute does not exist"))  

Dim att3 As XAttribute = root.Attribute("Att3")  
Dim v3 As String  
If att3 Is Nothing Then  
    v3 = Nothing  
Else  
    v3 = att3.Value  
End If  

Console.WriteLine("v3:{0}", IIf(v3 Is Nothing, "attribute does not exist", v3))  

Dim att4 As XAttribute = root.Attribute("Att4")  
Dim v4 As Nullable(Of Integer)  
If att4 Is Nothing Then  
    v4 = Nothing  
Else  
    v4 = Int32.Parse(att4.Value)  
End If  

Console.WriteLine("v4:{0}", IIf(v4.HasValue, v4, "attribute does not exist"))  

이 예제는 다음과 같은 출력을 생성합니다.

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  

설명

이 속성을 사용하여 특성 값을 얻거나 설정할 수 있습니다.

이 속성을 설정하면 및 이벤트가 발생 Changed 합니다 Changing .

값을 가져오고 특성이 없을 수 있는 경우 명시적 변환 연산자를 사용하고 특성(예: string 또는 Nullable<T> ) Int32에 특성을 할당하는 것이 더 편리합니다. 특성이 없으면 nullable 형식이 로 null설정됩니다. 이 속성을 사용하려면 먼저 메서드가 를 Attribute 반환 null하지 않는지 확인해야 합니다.

적용 대상

추가 정보