Pobieranie nieuporządkowanych węzłów na podstawie nazwy lub indeksu

Obiekt XmlNamedNodeMap jest opisany w specyfikacji World Wide Web Consortium (W3C) jako nazwa_węzłaMap i jest wymagany do obsługi nieuporządkowanego zestawu węzłów z możliwością odwołowania się do węzłów według ich nazwy lub indeksu. Jedynym sposobem uzyskania dostępu do elementu XmlNamedNodeMap jest zwracanie elementu XmlNamedNodeMap za pośrednictwem metody lub właściwości. Istnieją trzy metody lub właściwości zwracające element XmlNamedNodeMap:

  • XmlElement.Attributes

  • XmlDocumentType.Entities

  • XmlDocumentType.Notations

Na przykład właściwość XmlDocumentType.Entities pobiera kolekcję węzłów XmlEntity zadeklarowanych w deklaracji typu dokumentu. Ta kolekcja jest zwracana jako XmlNamedNodeMap i można iterować ją za pomocą właściwości Count i wyświetlić informacje o jednostce. Aby zapoznać się z przykładem iteracji za pomocą elementu XmlNamedNodeMap, zobacz Entities.

Element XmlAttributeCollection pochodzi z elementu XmlNamedNodeMap i można modyfikować tylko atrybuty, a notacje i jednostki są tylko do odczytu. Za pomocą atrybutów XmlNamedNodeMap można pobrać węzły dla tych atrybutów na podstawie ich nazw XML. Zapewnia to łatwą metodę manipulowania kolekcją atrybutów w węźle elementu. Można to porównać bezpośrednio z xmlNodeList, który implementuje również interfejs IEnumerable , ale z akcesorem indeksu, a nie ciągiem. Metody RemoveNamedItem i SetNamedItem są używane tylko dla klasy XmlAttributeCollection. Dodanie lub usunięcie z kolekcji atrybutów, niezależnie od tego, czy jest używana implementacja AttributeCollection , czy XmlNamedNodeMap , modyfikuje kolekcję atrybutów w elemecie . W poniższym przykładzie kodu pokazano, jak przenieść atrybut i utworzyć nowy atrybut.

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 +"'" );
  
    }  
}  

Aby wyświetlić dodatkowy przykład kodu, który pokazuje atrybut usuwany z atrybutu AttributeCollection, zobacz XmlNamedNodeMap.RemoveNamedItem Method (Metoda XmlNamedNodeMap.RemoveNamedItem). Aby uzyskać więcej informacji na temat metod i właściwości, zobacz Elementy członkowskie XmlNamedNodeMap.

Zobacz też