属性は要素のプロパティであり、要素の子ではありません。 この区別は、XML ドキュメント オブジェクト モデル (DOM) の兄弟ノード、親ノード、および子ノード間を移動するために使用されるメソッドのために重要です。 たとえば、 PreviousSibling メソッドと NextSibling メソッドは、要素から属性への移動や属性間の移動には使用されません。 代わりに、属性は要素のプロパティであり、要素によって所有され、ParentNode プロパティではなく OwnerElement プロパティを持ち、ナビゲーションの個別のメソッドを持ちます。
現在のノードが要素である場合は、 HasAttribute メソッドを使用して、要素に関連付けられている属性があるかどうかを確認します。 要素に属性が含まれることがわかっている場合は、属性にアクセスするための複数のメソッドがあります。 要素から 1 つの属性を取得するには、XmlElement の GetAttribute メソッドと GetAttributeNode メソッドを使用するか、すべての属性をコレクションに取得できます。 コレクションの取得は、コレクションを反復処理する必要がある場合に便利です。 要素のすべての属性が必要な場合は、要素の Attributes プロパティを使用して、すべての属性をコレクションに取得します。
すべての属性をコレクションに取得する
要素ノードのすべての属性をコレクションに格納する場合は、 XmlElement.Attributes プロパティを呼び出します。 これは、要素のすべての属性を含む XmlAttributeCollection を取得します。 XmlAttributeCollection クラスは、XmlNamedNode マップから継承します。 そのため、コレクションで使用できるメソッドとプロパティには、ItemOf プロパティや Append メソッドなど、XmlAttributeCollection クラスに固有のメソッドとプロパティに加えて、名前付きノード マップで使用できるメソッドとプロパティが含まれます。 属性コレクション内の各項目は 、XmlAttribute ノードを表します。 要素の属性の数を検索するには、 XmlAttributeCollection を取得し、 Count プロパティを使用して、コレクション内の XmlAttribute ノードの数を確認します。
次のコード例は、属性コレクションを取得し、ループ インデックスに Count メソッドを使用して、それを反復処理する方法を示しています。 次に、コレクションから 1 つの属性を取得し、その値を表示する方法を示します。
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5' misc='sale item'>" & _
"<title>The Handmaid's Tale</title>" & _
"<price>14.95</price>" & _
"</book>")
' Move to an element.
Dim myElement As XmlElement = doc.DocumentElement
' Create an attribute collection from the element.
Dim attrColl As XmlAttributeCollection = myElement.Attributes
' Show the collection by iterating over it.
Console.WriteLine("Display all the attributes in the collection...")
Dim i As Integer
For i = 0 To attrColl.Count - 1
Console.Write("{0} = ", attrColl.ItemOf(i).Name)
Console.Write("{0}", attrColl.ItemOf(i).Value)
Console.WriteLine()
Next
' Retrieve a single attribute from the collection; specifically, the
' attribute with the name "misc".
Dim attr As XmlAttribute = attrColl("misc")
' Retrieve the value from that attribute.
Dim miscValue As String = attr.InnerXml
Console.WriteLine("Display the attribute information.")
Console.WriteLine(miscValue)
End Sub
End Class
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5' misc='sale item'>" +
"<title>The Handmaid's Tale</title>" +
"<price>14.95</price>" +
"</book>");
// Move to an element.
XmlElement myElement = doc.DocumentElement;
// Create an attribute collection from the element.
XmlAttributeCollection attrColl = myElement.Attributes;
// Show the collection by iterating over it.
Console.WriteLine("Display all the attributes in the collection...");
for (int i = 0; i < attrColl.Count; i++)
{
Console.Write("{0} = ", attrColl[i].Name);
Console.Write("{0}", attrColl[i].Value);
Console.WriteLine();
}
// Retrieve a single attribute from the collection; specifically, the
// attribute with the name "misc".
XmlAttribute attr = attrColl["misc"];
// Retrieve the value from that attribute.
String miscValue = attr.InnerXml;
Console.WriteLine("Display the attribute information.");
Console.WriteLine(miscValue);
}
}
この例では、次の出力が表示されます。
アウトプット
コレクション内のすべての属性を表示します。
genre = novel
ISBN = 1-861001-57-5
misc = sale item
Display the attribute information.
sale item
属性コレクション内の情報は、名前またはインデックス番号で取得できます。 上の例は、名前でデータを取得する方法を示しています。 次の例では、インデックス番号でデータを取得する方法を示します。
XmlAttributeCollection はコレクションであり、名前またはインデックスで反復処理できるため、この例では、0 から始まるインデックスを使用してコレクションから最初の属性を選択し、次のファイル baseuri.xmlを入力として使用します。
インプット
<!-- XML fragment -->
<book genre="novel">
<title>Pride And Prejudice</title>
</book>
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
' Create the XmlDocument.
Dim doc As New XmlDocument()
doc.Load("http://localhost/baseuri.xml")
' Display information on the attribute node. The value
' returned for BaseURI is 'http://localhost/baseuri.xml'.
Dim attr As XmlAttribute = doc.DocumentElement.Attributes(0)
Console.WriteLine("Name of the attribute: {0}", attr.Name)
Console.WriteLine("Base URI of the attribute: {0}", attr.BaseURI)
Console.WriteLine("The value of the attribute: {0}", attr.InnerText)
End Sub 'Main
End Class 'Sample
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("http://localhost/baseuri.xml");
// Display information on the attribute node. The value
// returned for BaseURI is 'http://localhost/baseuri.xml'.
XmlAttribute attr = doc.DocumentElement.Attributes[0];
Console.WriteLine("Name of the attribute: {0}", attr.Name);
Console.WriteLine("Base URI of the attribute: {0}", attr.BaseURI);
Console.WriteLine("The value of the attribute: {0}", attr.InnerText);
}
}
個々の属性ノードの取得
要素から 1 つの属性ノードを取得するには、 XmlElement.GetAttributeNode メソッドが使用されます。 XmlAttribute 型のオブジェクトを返します。 XmlAttribute を作成すると、System.Xml.XmlAttribute クラスで使用できるすべてのメソッドとプロパティが、OwnerElement の検索など、そのオブジェクトで使用できるようになります。
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5' misc='sale item'>" & _
"<title>The Handmaid's Tale</title>" & _
"<price>14.95</price>" & _
"</book>")
' Move to an element.
Dim root As XmlElement
root = doc.DocumentElement
' Get an attribute.
Dim attr As XmlAttribute
attr = root.GetAttributeNode("ISBN")
' Display the value of the attribute.
Dim attrValue As String
attrValue = attr.InnerXml
Console.WriteLine(attrValue)
End Sub
End Class
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861003-78' misc='sale item'>" +
"<title>The Handmaid's Tale</title>" +
"<price>14.95</price>" +
"</book>");
// Move to an element.
XmlElement root = doc.DocumentElement;
// Get an attribute.
XmlAttribute attr = root.GetAttributeNode("ISBN");
// Display the value of the attribute.
String attrValue = attr.InnerXml;
Console.WriteLine(attrValue);
}
}
前の例に示すように、1 つの属性ノードが属性コレクションから取得されるようにすることもできます。 次のコード例は、1 行のコードを記述して、XML ドキュメント ツリーのルート ( DocumentElement プロパティとも呼ばれます) からインデックス番号によって 1 つの属性を取得する方法を示しています。
XmlAttribute attr = doc.DocumentElement.Attributes[0];
こちらも参照ください
.NET