语言

按名称或索引无序检索节点

XmlNamedNodeMap 在万维网联合会(W3C)规范中描述为 NamedNodeMap,并且需要处理一组无序节点,并且能够按名称或索引引用节点。 访问 XmlNamedNodeMap 的唯一方法是通过方法或属性返回 XmlNamedNodeMap 。 有三种返回 XmlNamedNodeMap 的方法或属性:

  • 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 成员。

另请参阅