Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Die XmlNamedNodeMap wird in der World Wide Web Consortium (W3C)-Spezifikation als NamedNodeMap beschrieben und ist erforderlich, um einen ungeordneten Satz von Knoten mit der Möglichkeit zu behandeln, nach ihrem Namen oder Index auf Knoten zu verweisen. Sie haben nur Zugriff auf eine XmlNamedNodeMap , wenn eine XmlNamedNodeMap über eine Methode oder Eigenschaft zurückgegeben wird. Es gibt drei Methoden oder Eigenschaften, die eine XmlNamedNodeMap zurückgeben:
XmlElement.Attributes
XmlDocumentType.Entities
Xml-Dokumenttyp.Notationen
Beispielsweise ruft die XmlDocumentType.Entities-Eigenschaft die Auflistung von XmlEntity-Knoten ab, die in der Dokumenttypdeklaration deklariert sind. Diese Auflistung wird als XmlNamedNodeMap zurückgegeben, und Sie können die Auflistung mit der Verwendung der Count-Eigenschaft durchlaufen und Entitätsinformationen anzeigen. Ein Beispiel für das Iterieren durch eine XmlNamedNodeMap finden Sie unter Entities.
Die XmlAttributeCollection wird von XmlNamedNodeMap abgeleitet, und nur Attribute können geändert werden, während Notationen und Entitäten schreibgeschützt sind. Mithilfe der XmlNamedNodeMap für die Attribute können Sie Knoten für diese Attribute basierend auf ihren XML-Namen abrufen. Dies bietet eine einfache Methode zum Bearbeiten der Sammlung von Attributen auf einem Elementknoten. Dies kann direkt mit XmlNodeList kontrastiert werden, das auch die IEnumerable-Schnittstelle implementiert, aber mit einem Indexaccessor anstelle einer Zeichenfolge. Die Methoden RemoveNamedItem und SetNamedItem werden nur für eine XmlAttributeCollection verwendet. Durch Hinzufügen oder Entfernen aus einer Attributauflistung, unabhängig davon, ob die AttributCollection - oder xmlNamedNodeMap-Implementierung verwendet wird, wird die Attributauflistung für das Element geändert. Das folgende Codebeispiel zeigt, wie Sie ein Attribut verschieben und ein neues Attribut erstellen.
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 +"'" );
}
}
Ein zusätzliches Codebeispiel, in dem ein Attribut angezeigt wird, das aus einer AttributeCollection entfernt wird, finden Sie unter XmlNamedNodeMap.RemoveNamedItem-Methode. Weitere Informationen zu den Methoden und Eigenschaften finden Sie unter "XmlNamedNodeMap"-Elemente.