【发布时间】:2020-01-13 19:17:35
【问题描述】:
我正在解析一个 XML 文档,该文档在具有相同名称的不同节点下具有信息。截至目前,当我使用 xpath 时,我返回了父节点 (2) 和我正在寻找的节点 (6) 的总数,但实际上每个节点下都有 5 个。 xml 看起来与此类似:
<library>
<rack>13
<shelf>shelf 1
<book>
<title>title1</title>
<location>e6</location>
</book>
<book>
<title>title2</title>
<location>e7</location>
</book >
<book>
<title>title3</title>
<location>e8</location>
</book >
</shelf>
</rack>
<rack>13
<shelf>shelf 2
<book>
<title>title4</title>
<location>h5</location>
</book>
<book>
<title>title5</title>
<location>h6</location>
</book>
<book>
<title>title6</title>
<location>h7</location >
</book>
</shelf>
</rack>
</library>
我目前正在使用 xpath 来读取其中的计数为 6。
xmlNodeList library = doc.SelectNodes(//library);
xmlNodeList shelf= doc.SelectNodes(//library//shelf);
xmlNodeList location = doc.SelectNodes(//library//book//location);
if (library != null)
{
for (int I=0; I < shelf.count; I++) //this should be 2
{
for (int j=0; j < location.count; j++) //this should be 6
{
Console.Writeline(shelf[j].innertext + " " + location[j].innertext);
}
}
}
所以问题是我明白了
shelf 1 e6
shelf 1 e7
shelf 1 e8
shelf 1 h5
shelf 1 h6
shelf 1 h7
shelf 2 e6
shelf 2 e7
shelf 2 e8
shelf 2 h5
shelf 2 h6
shelf 2 h7
但我想看到更多类似
的东西shelf 1 e6
shelf 1 e7
shelf 1 e8
shelf 2 h5
shelf 2 h6
shelf 2 h7
为了清晰起见,尝试获取实际的 XML 文档
【问题讨论】: