XmlNamedNodeMap は、World Wide Web Consortium (W3C) 仕様で NamedNodeMap として記述されており、ノードの名前またはインデックスでノードを参照する機能を持つ順序のないノード セットを処理する必要があります。 XmlNamedNodeMap にアクセスできる唯一の方法は、XmlNamedNodeMap がメソッドまたはプロパティを介して返される場合です。 XmlNamedNodeMap を返す 3 つのメソッドまたはプロパティがあります。
XmlElement.Attributes
XmlDocumentType.Entities
XmlDocumentType.Notations
たとえば、 XmlDocumentType.Entities プロパティは、ドキュメント型宣言で宣言された XmlEntity ノードのコレクションを取得します。 このコレクションは XmlNamedNodeMap として返され、 Count プロパティを使用してコレクションを反復処理し、エンティティ情報を表示できます。 XmlNamedNodeMap の反復処理の例については、Entitiesを参照してください。
XmlAttributeCollection は XmlNamedNodeMap から派生し、属性のみが変更可能ですが、表記とエンティティは読み取り専用です。 属性に XmlNamedNodeMap を使用すると、それらの属性のノードを XML 名に基づいて取得できます。 これにより、要素ノードの属性のコレクションを操作するための簡単なメソッドが提供されます。 これは、IEnumerable インターフェイスも実装する XmlNodeList と直接対照的ですが、文字列ではなくインデックス アクセサーを使用します。 RemoveNamedItem メソッドと SetNamedItem メソッドは、XmlAttributeCollection に対してのみ使用されます。 AttributeCollection または XmlNamedNodeMap 実装を使用しているかどうかに関係なく、属性コレクションを追加または削除すると、要素の属性コレクションが変更されます。 次のコード例は、属性を移動して新しい属性を作成する方法を示しています。
Imports System
Imports System.Xml
Class test
Public Shared Sub Main()
Dim doc As New XmlDocument()
doc.LoadXml("<root> <child1 attr1='val1' attr2='val2'> text1 </child1> <child2 attr3='val3'> text2 </child2> </root> ")
' Get the attributes of node "child2 "
Dim ac As XmlAttributeCollection = doc.DocumentElement.ChildNodes(1).Attributes
' Print out the number of attributes and their names.
Console.WriteLine(("Number of Attributes: " + ac.Count))
Dim i As Integer
For i = 0 To ac.Count - 1
Console.WriteLine((i + 1 + ". Attribute Name: '" + ac(i).Name + "' Attribute Value: '" + ac(i).Value + "'"))
Next i
' Get the 'attr1' from child1.
Dim attr As XmlAttribute = doc.DocumentElement.ChildNodes(0).Attributes(0)
' Add this attribute to the attributecollection "ac".
ac.SetNamedItem(attr)
''attr1' will be removed from 'child1' and added to 'child2'.
' Print out the number of attributes and their names.
Console.WriteLine(("Number of Attributes: " + ac.Count))
For i = 0 To ac.Count - 1
Console.WriteLine((i + 1 + ". Attribute Name: '" + ac(i).Name + "' Attribute Value: '" + ac(i).Value + "'"))
Next i
' Create a new attribute and add to the collection.
Dim attr2 As XmlAttribute = doc.CreateAttribute("attr4")
attr2.Value = "val4"
ac.SetNamedItem(attr2)
' Print out the number of attributes and their names.
Console.WriteLine(("Number of Attributes: " + ac.Count))
For i = 0 To ac.Count - 1
Console.WriteLine((i + 1 + ". Attribute Name: '" + ac(i).Name + "' Attribute Value: '" + ac(i).Value + "'"))
Next i
End Sub 'Main
End Class 'test
using System;
using System.Xml;
class test {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml( "<root> <child1 attr1='val1' attr2='val2'> text1 </child1> <child2 attr3='val3'> text2 </child2> </root> " );
// Get the attributes of node "child2"
XmlAttributeCollection ac = doc.DocumentElement.ChildNodes[1].Attributes;
// Print out the number of attributes and their names.
Console.WriteLine( "Number of Attributes: "+ac.Count );
for( int i = 0; i < ac.Count; i++ )
Console.WriteLine( (i+1) + ". Attribute Name: '" +ac[i].Name+ "' Attribute Value: '"+ ac[i].Value +"'" );
// Get the 'attr1' from child1.
XmlAttribute attr = doc.DocumentElement.ChildNodes[0].Attributes[0];
// Add this attribute to the attributecollection "ac".
ac.SetNamedItem( attr );
// 'attr1' will be removed from 'child1' and added to 'child2'.
// Print out the number of attributes and their names.
Console.WriteLine( "Number of Attributes: "+ac.Count );
for( int i = 0; i < ac.Count; i++ )
Console.WriteLine( (i+1) + ". Attribute Name: '" +ac[i].Name+ "' Attribute Value: '"+ ac[i].Value +"'" );
// Create a new attribute and add to the collection.
XmlAttribute attr2 = doc.CreateAttribute( "attr4" );
attr2.Value = "val4";
ac.SetNamedItem( attr2 );
// Print out the number of attributes and their names.
Console.WriteLine( "Number of Attributes: "+ac.Count );
for( int i = 0; i < ac.Count; i++ )
Console.WriteLine( (i+1) + ". Attribute Name: '" +ac[i].Name+ "' Attribute Value: '"+ ac[i].Value +"'" );
}
}
AttributeCollection から削除される属性を示す追加のコード例については、「XmlNamedNodeMap.RemoveNamedItem メソッド」を参照してください。 メソッドとプロパティの詳細については、「 XmlNamedNodeMap メンバー」を参照してください。
こちらも参照ください
.NET