C# 获取每个xml节点的XmlNode值

C# 获取每个xml节点的XmlNode值,c#,xml,xpath,xmlnode,xmlnodelist,C#,Xml,Xpath,Xmlnode,Xmlnodelist,我正在使用下面的xml文件 <?xml version="1.0" encoding="UTF-8"?> <bookstore xmlns="urn:newbooks-schema"> <book> <title>Books</title> <price>20.00</price> <attribute>

我正在使用下面的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns="urn:newbooks-schema">  
  <book>
    <title>Books</title>
    <price>20.00</price>
  <attribute>
      <fieldName>Books</fieldName>
      <attributeStyle>ValueSet</attributeStyle>
      <valueset>
        <id>Part 1</id>
        <values>
          <displayName>Lord of the Rings</displayName>
        </values>
      </valueset>
    </attribute>
  </book>  
  
  <book>
    <title>Books</title>
    <price>20.00</price>
    <attribute>
      <fieldName>Books</fieldName>
      <valueset>
        <id>Part 1</id>
        <values>
          <displayName>Harry Potter</displayName>
        </values>
      </valueset>
    </attribute>
  </book>
</bookstore>

我稍微重构了代码,为变量提供了更清晰的名称

// Here the previous code is unchanged.

var books = root.SelectNodes("bk:book[bk:title='Books']", nsmgr);
Console.WriteLine(books.Count);

foreach (XmlNode book in books)
{
    var valueset = book.SelectSingleNode(".//bk:valueset", nsmgr);
    var id = valueset.SelectSingleNode("./bk:id", nsmgr).InnerText;
    var displayName = valueset.SelectSingleNode(".//bk:displayName", nsmgr).InnerText;

    Console.WriteLine(id);
    Console.WriteLine(displayName);
}

请标记一种语言。
// Here the previous code is unchanged.

var books = root.SelectNodes("bk:book[bk:title='Books']", nsmgr);
Console.WriteLine(books.Count);

foreach (XmlNode book in books)
{
    var valueset = book.SelectSingleNode(".//bk:valueset", nsmgr);
    var id = valueset.SelectSingleNode("./bk:id", nsmgr).InnerText;
    var displayName = valueset.SelectSingleNode(".//bk:displayName", nsmgr).InnerText;

    Console.WriteLine(id);
    Console.WriteLine(displayName);
}