Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
World Wide Web Consortium (W3C) XML Document Object Model (DOM) také popisuje NodeList, který dokáže zpracovat uspořádaný seznam uzlů, na rozdíl od neuspořádané sady zpracovávané XmlNamedNodeMap. NodeList v rozhraní Microsoft .NET Framework se nazývá XmlNodeList. Metody a vlastnosti, které vracejí XmlNodeList jsou:
XmlNode.ChildNodes
XmlDocument.GetElementsByTagName
XmlElement.GetElementsByTagName
XmlNode.SelectNodes
XmlNodeList má count vlastnost, kterou lze použít k zápisu smyčky iterace nad uzly v XmlNodeList, jak je znázorněno v následující ukázce kódu:
Dim doc as XmlDocument = new XmlDocument()
doc.Load("books.xml")
' Retrieve all book titles.
Dim root as XmlElement = doc.DocumentElement
Dim elemList as XmlNodeList = root.GetElementsByTagName("title")
Dim i as integer
for i=0 to elemList.Count-1
' Display all book titles in the Node List.
Console.WriteLine(elemList.ItemOf(i).InnerXml)
next
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
// Retrieve all book titles.
XmlElement root = doc.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("title");
for (int i=0; i < elemList.Count; i++)
{
// Display all book titles in the Node List.
Console.WriteLine(elemList[i].InnerXml);
}
Kromě vlastnosti Count existuje metoda GetEnumerator, která poskytuje iteraci ve stylu foreach nad kolekcí uzlů v XmlNodeList. Následující příklad kódu ukazuje použití foreach příkazu.
Dim doc As New XmlDocument()
doc.Load("books.xml")
' Get book titles.
Dim root As XmlElement = doc.DocumentElement
Dim elemList As XmlNodeList = root.GetElementsByTagName("title")
Dim ienum As IEnumerator = elemList.GetEnumerator()
' Loop over the XmlNodeList using the enumerator ienum
While ienum.MoveNext()
' Display the book title.
Dim title As XmlNode = CType(ienum.Current, XmlNode)
Console.WriteLine(title.InnerText)
End While
{
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
// Get book titles.
XmlElement root = doc.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("title");
IEnumerator ienum = elemList.GetEnumerator();
// Loop over the XmlNodeList using the enumerator ienum
while (ienum.MoveNext())
{
// Display the book title.
XmlNode title = (XmlNode) ienum.Current;
Console.WriteLine(title.InnerText);
}
}
Další informace o metodách a vlastnostech, které jsou k dispozici v XmlNodeList naleznete v tématu XmlNodeList.