Ada veya Dizine Göre Sırasız Düğüm Alma
XmlNamedNodeMap, World Wide Web Consortium (W3C) belirtiminde NamedNodeMap olarak tanımlanır ve düğümlere adlarına veya dizinlerine göre başvurabilme özelliğine sahip sıralanmamış bir düğüm kümesini işlemek için gereklidir. XmlNamedNodeMap'e erişmenin tek yolu, bir XmlNamedNodeMap'in bir yöntem veya özellik aracılığıyla döndürülür olmasıdır. XmlNamedNodeMap döndüren üç yöntem veya özellik vardır:
XmlElement.Attributes
XmlDocumentType.Entities
XmlDocumentType.Notations
Örneğin, XmlDocumentType.Entities özelliği belge türü bildiriminde bildirilen XmlEntity düğümleri koleksiyonunu alır. Bu koleksiyon bir XmlNamedNodeMap olarak döndürülür ve Count özelliğini kullanarak koleksiyonda yineleme yapabilir ve varlık bilgilerini görüntüleyebilirsiniz. XmlNamedNodeMap aracılığıyla yineleme örneği için bkzEntities. .
XmlAttributeCollection, XmlNamedNodeMap'ten türetilir ve yalnızca öznitelikler değiştirilebilirken, notasyonlar ve varlıklar salt okunurdur. Öznitelikler için XmlNamedNodeMap kullanarak, bu özniteliklerin XML adlarına göre düğümleri alabilirsiniz. Bu, bir öğe düğümündeki öznitelik koleksiyonunu işlemek için kolay bir yöntem sağlar. Bu, IEnumerable arabirimini de uygulayan XmlNodeList ile, ancak dize yerine dizin erişimcisiyle doğrudan karşıtlık sağlayabilir. RemoveNamedItem ve SetNamedItem yöntemleri yalnızca xmlAttributeCollection için kullanılır. AttributeCollection veya XmlNamedNodeMap uygulamasını kullanarak öznitelik koleksiyonu ekleme veya kaldırma, öğesindeki öznitelik koleksiyonunu değiştirir. Aşağıdaki kod örneği, bir özniteliğin nasıl taşınacağını ve yeni bir özniteliğin nasıl oluşturulacağını gösterir.
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'dan kaldırılan özniteliği gösteren ek kod örneğini görmek için bkz. XmlNamedNodeMap.RemoveNamedItem Yöntemi. Yöntemler ve özellikler hakkında daha fazla bilgi için bkz . XmlNamedNodeMap Üyeleri.
Ayrıca bkz.
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin