語言

依名稱或索引擷取未排序的節點

XmlNamedNodeMap 在萬維網聯合會 (W3C) 規格中描述為 NamedNodeMap,而且必須以名稱或索引來處理未排序的節點集,且能夠依其名稱或索引參考節點。 您存取 XmlNamedNodeMap 的唯一方式是當 XmlNamedNodeMap 透過方法或屬性傳回時。 傳回 XmlNamedNodeMap 的方法或屬性有三種:

  • Xml元素屬性 (XmlElement.Attributes)

  • XmlDocumentType.Entities

  • XmlDocumentType.Notations

例如, XmlDocumentType.Entities 屬性會取得在檔類型宣告中宣告的 XmlEntity 節點集合。 此集合會以 XmlNamedNodeMap 傳回,而且您可以使用 Count 屬性來逐一查看集合,並顯示實體資訊。 如需逐一查看 XmlNamedNodeMap 的範例,請參閱 Entities。

XmlAttributeCollection 衍生自 XmlNamedNodeMap,而且只能修改屬性,而表示法和實體則為唯讀。 針對屬性使用 XmlNamedNodeMap ,您可以根據這些屬性的 XML 名稱取得這些屬性的節點。 這提供了一個簡單的方法來操作元素節點上的屬性集合。 這可以直接與 XmlNodeList 形成對比,它也會實作 IEnumerable 介面,但使用索引存取子而非字串。 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 成員。

另請參閱