XmlNodeList.Item(Int32) 方法

定义

检索给定索引处的节点。

C#
public abstract System.Xml.XmlNode Item (int index);
C#
public abstract System.Xml.XmlNode? Item (int index);

参数

index
Int32

节点列表中从零开始的索引。

返回

XmlNode

XmlNode 与集合中的指定索引. 如果 index 大于或等于列表中的节点数,则这返回 null

示例

以下示例获取并显示第二个节点。XmlNodeList

C#
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<items>" +
                 "  <item>First item</item>" +
                 "  <item>Second item</item>" +
                 "</items>");

     //Get and display the last item node.
     XmlElement root = doc.DocumentElement;
     XmlNodeList nodeList = root.GetElementsByTagName("item");
     Console.WriteLine(nodeList.Item(1).InnerXml);
  }
}

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1
UWP 10.0