この記事では、要素の値を取得する方法について説明します。 この値を取得するには、主に次の 2 つの方法があります。
XElementまたはXAttributeを目的の型にキャストします。 明示的な変換演算子は、要素または属性の内容を指定された型に変換し、変数に割り当てます。
XElement.ValueプロパティまたはXAttribute.Valueプロパティを使用します。 これらのプロパティを使用して値を設定することもできます。
C# では、一般的にキャストの方が優れたアプローチです。 要素または属性を null 許容値型にキャストすると、存在しない可能性がある要素 (または属性) の値を取得するときに、コードの記述が簡単になります。 この記事の最後の例では、これを示します。 ただし、キャストを使用して要素の内容を設定することはできませんが、XElement.Value プロパティを使用すれば可能です。
Visual Basic では、 XElement.Value プロパティを使用することをお勧めします。
文字列キャストの例
要素の値を取得するには、 XElement オブジェクトを目的の型にキャストします。 次のように、要素を文字列にキャストできます。
XElement e = new XElement("StringElement", "abcde");
Console.WriteLine(e);
Console.WriteLine("Value of e:" + (string)e);
Dim e As XElement = <StringElement>abcde</StringElement>
Console.WriteLine(e)
Console.WriteLine("Value of e:" & e.Value)
この例を実行すると、次の出力が生成されます。
<StringElement>abcde</StringElement>
Value of e:abcde
整数キャストの例
文字列以外の型に要素をキャストすることもできます。 たとえば、整数を含む要素がある場合は、次のコードに示すように、 intにキャストできます。
XElement e = new XElement("Age", "44");
Console.WriteLine(e);
Console.WriteLine("Value of e:" + (int)e);
Dim e As XElement = <Age>44</Age>
Console.WriteLine(e)
Console.WriteLine("Value of e:" & CInt(e))
この例を実行すると、次の出力が生成されます。
<Age>44</Age>
Value of e:44
LINQ to XML には、次のデータ型に対する明示的なキャスト演算子が用意されています: string、 bool、 bool?、 int、 int?、 uint、 uint?、 long、 long?、 ulong、 ulong?float、 float?、 double、 double?、 decimal、 decimal?、 DateTime、 DateTime?、 TimeSpan、 TimeSpan?、 GUID、および GUID?。
LINQ to XML では、 XAttribute オブジェクトに対して同じキャスト演算子が提供されます。
Value プロパティの使用例
Value プロパティを使用して、要素の内容を取得できます。
XElement e = new XElement("StringElement", "abcde");
Console.WriteLine(e);
Console.WriteLine("Value of e:" + e.Value);
Dim e As XElement = <StringElement>abcde</StringElement>
Console.WriteLine(e)
Console.WriteLine("Value of e:" & e.Value)
この例を実行すると、次の出力が生成されます。
<StringElement>abcde</StringElement>
Value of e:abcde
要素が存在しない可能性がある例
要素が存在するかどうかわからない場合でも、要素の値を取得しようとする場合があります。 この場合、キャストされた要素を null 許容参照型または null 許容値型に割り当てると、要素が存在しない場合、割り当てられた変数は null (C#) または nothing (Visual Basic) に設定されます。 次のコードは、要素が存在しない可能性がある場合、キャストを使う方が Value プロパティを使うよりも簡単であることを示しています。
XElement root = new XElement("Root",
new XElement("Child1", "child 1 content"),
new XElement("Child2", "2")
);
// The following assignments show why it's easier to use
// casting when the element might or might not exist.
string c1 = (string)root.Element("Child1");
Console.WriteLine("c1:{0}", c1 == null ? "element doesn't exist" : c1);
int? c2 = (int?)root.Element("Child2");
Console.WriteLine("c2:{0}", c2 == null ? "element doesn't exist" : c2.ToString());
string c3 = (string)root.Element("Child3");
Console.WriteLine("c3:{0}", c3 == null ? "element doesn't exist" : c3);
int? c4 = (int?)root.Element("Child4");
Console.WriteLine("c4:{0}", c4 == null ? "element doesn't exist" : c4.ToString());
Console.WriteLine();
// The following assignments show the required code when using
// the Value property when the element might or might not exist.
// Notice that this is more difficult than the casting approach.
XElement e1 = root.Element("Child1");
string v1;
if (e1 == null)
v1 = null;
else
v1 = e1.Value;
Console.WriteLine("v1:{0}", v1 == null ? "element doesn't exist" : v1);
XElement e2 = root.Element("Child2");
int? v2;
if (e2 == null)
v2 = null;
else
v2 = Int32.Parse(e2.Value);
Console.WriteLine("v2:{0}", v2 == null ? "element doesn't exist" : v2.ToString());
XElement e3 = root.Element("Child3");
string v3;
if (e3 == null)
v3 = null;
else
v3 = e3.Value;
Console.WriteLine("v3:{0}", v3 == null ? "element doesn't exist" : v3);
XElement e4 = root.Element("Child4");
int? v4;
if (e4 == null)
v4 = null;
else
v4 = Int32.Parse(e4.Value);
Console.WriteLine("v4:{0}", v4 == null ? "element doesn't exist" : v4.ToString());
Dim root As XElement = <Root>
<Child1>child 1 content</Child1>
<Child2>2</Child2>
</Root>
' The following assignments show why it's easier to use
' casting when the element might or might not exist.
Dim c1 As String = CStr(root.Element("Child1"))
Console.WriteLine("c1:{0}", IIf(c1 Is Nothing, "element doesn't exist", c1))
Dim c2 As Nullable(Of Integer) = CType(root.Element("Child2"), Nullable(Of Integer))
Console.WriteLine("c2:{0}", IIf(Not (c2.HasValue), "element doesn't exist", c2.ToString()))
Dim c3 As String = CStr(root.Element("Child3"))
Console.WriteLine("c3:{0}", IIf(c3 Is Nothing, "element doesn't exist", c3))
Dim c4 As Nullable(Of Integer) = CType(root.Element("Child4"), Nullable(Of Integer))
Console.WriteLine("c4:{0}", IIf(Not (c4.HasValue), "element doesn't exist", c4.ToString()))
Console.WriteLine()
' The following assignments show the required code when using
' the Value property when the attribute might or might not exist.
' Notice that this is more difficult than the casting approach.
Dim e1 As XElement = root.Element("Child1")
Dim v1 As String
If (e1 Is Nothing) Then
v1 = Nothing
Else
v1 = e1.Value
End If
Console.WriteLine("v1:{0}", IIf(v1 Is Nothing, "element doesn't exist", v1))
Dim e2 As XElement = root.Element("Child2")
Dim v2 As Nullable(Of Integer)
If (e2 Is Nothing) Then
v2 = Nothing
Else
v2 = e2.Value
End If
Console.WriteLine("v2:{0}", IIf(Not (v2.HasValue), "element doesn't exist", v2))
Dim e3 As XElement = root.Element("Child3")
Dim v3 As String
If (e3 Is Nothing) Then
v3 = Nothing
Else
v3 = e3.Value
End If
Console.WriteLine("v3:{0}", IIf(v3 Is Nothing, "element doesn't exist", v3))
Dim e4 As XElement = root.Element("Child4")
Dim v4 As Nullable(Of Integer)
If (e4 Is Nothing) Then
v4 = Nothing
Else
v4 = e4.Value
End If
Console.WriteLine("v4:{0}", IIf(Not (v4.HasValue), "element doesn't exist", v4))
この例を実行すると、次の出力が生成されます。
c1:child 1 content
c2:2
c3:element doesn't exist
c4:element doesn't exist
v1:child 1 content
v2:2
v3:element doesn't exist
v4:element doesn't exist
一般に、要素と属性の内容を取得するためにキャストすることで、より簡単なコードを記述できます。
こちらも参照ください
.NET