Condividi tramite


Recupero di nodi non ordinati in base al nome o all'indice

XmlNamedNodeMap è descritto nella specifica World Wide Web Consortium (W3C) come NamedNodeMap ed è necessario per gestire un set non ordinato di nodi con la possibilità di fare riferimento ai nodi in base al nome o all'indice. L'unico modo in cui si ha accesso a un oggetto XmlNamedNodeMap è quando viene restituito un oggetto XmlNamedNodeMap tramite un metodo o una proprietà. Esistono tre metodi o proprietà che restituiscono xmlNamedNodeMap:

  • XmlElement.Attributes

  • XmlDocumentType.Entities

  • XmlDocumentType.Notations

Ad esempio, la proprietà XmlDocumentType.Entities ottiene la raccolta di nodi XmlEntity dichiarati nella dichiarazione del tipo di documento. Questa raccolta viene restituita come XmlNamedNodeMap ed è possibile scorrere la raccolta con l'uso della proprietà Count e visualizzare le informazioni sull'entità. Per un esempio di iterazione tramite xmlNamedNodeMap, vedere Entities.

XmlAttributeCollection è derivato da XmlNamedNodeMap e solo gli attributi sono modificabili, mentre le notazioni e le entità sono di sola lettura. Usando XmlNamedNodeMap per gli attributi, è possibile ottenere nodi per tali attributi in base ai relativi nomi XML. In questo modo è possibile modificare facilmente la raccolta di attributi in un nodo di elemento. Ciò può essere contrastato direttamente con XmlNodeList, che implementa anche l'interfaccia IEnumerable , ma con una funzione di accesso all'indice anziché una stringa. I metodi RemoveNamedItem e SetNamedItem vengono usati solo per xmlAttributeCollection. L'aggiunta o la rimozione da una raccolta di attributi, indipendentemente dall'uso di AttributeCollection o dall'implementazione XmlNamedNodeMap , modifica la raccolta di attributi nell'elemento . Nell'esempio di codice seguente viene illustrato come spostare un attributo e creare un nuovo attributo.

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

Per un esempio di codice aggiuntivo che mostra un attributo rimosso da attributeCollection, vedere Metodo XmlNamedNodeMap.RemoveNamedItem. Per altre informazioni sui metodi e sulle proprietà, vedere Membri XmlNamedNodeMap.

Vedere anche