我想按照索引选择xml节点。
例如 
int cnt=5; 
XmlNode node = doc.SelectSingleNode("//book[cnt]");//这样不行 
XmlNode node = doc.SelectSingleNode("//book['" + cnt + "']");//这样也不行 
XmlNode node = doc.SelectSingleNode("//book[5]");//只有这样可行
该怎么做?

解决方案 »

  1.   

    那你的获取到node集合,如childnodes,selectnodes
      

  2.   

    XmlDocument.SelectSingleNode(string xpath);参数传进去的是xpath格式.
    int cnt=5;  
    XmlNode node = doc.SelectSingleNode(string.Contact( "//book[", cnt , "]");这样应该行...
      

  3.   

    单引号去掉
                int index = 5;
                XmlDocument xml = new XmlDocument();
                xml.Load(@"E:\a.xml");
                XmlNode node = xml.SelectSingleNode("/a/b[" + index + "]");
                Console.WriteLine(node.InnerText);
    <?xml version="1.0" encoding="utf-8" ?>
    <a>
    <b>1</b>
    <b>2</b>
    <b>3</b>
    <b>4</b>
    <b>5</b>
    <b>6</b>
    <b>7</b>
    </a>