SelectSingleNode() 与 XPath C# 失败
我从 Wireshark 导出了一个 XML 文件,想要选择实际帧的编号
该文件的结构是这样的
<packet>
<proto>
...
</proto>
....
<proto>
<field name="frame.number" show="1">
</proto>
</packet>
<packet>
<proto>
...
</proto>
....
<proto>
<field name="frame.number" show="2">
</proto>
</packet>
...依此类推...
我使用此代码来选择数据包/字段
XmlNodeList packages = xmlDoc.SelectNodes("//packet");
foreach (XmlNode packet in packages) {
string frameNumber = packet.SelectSingleNode("//field[@name='frame.number']").
Attributes["show"].Value;
Console.WriteLine(frameNumber);
}
如果我通过调试在代码中,它总是选择具有正确属性的正确节点。但每次迭代都会打印出一个“1”。
有人怀疑这是什么失败吗?我在互联网上没有找到任何关于此故障的信息,
非常感谢!
i got a XML File as export from Wireshark and want to select the Number of the actual frame
The structure of this file is like this
<packet>
<proto>
...
</proto>
....
<proto>
<field name="frame.number" show="1">
</proto>
</packet>
<packet>
<proto>
...
</proto>
....
<proto>
<field name="frame.number" show="2">
</proto>
</packet>
...and so on...
I use this code to select the packets/fields
XmlNodeList packages = xmlDoc.SelectNodes("//packet");
foreach (XmlNode packet in packages) {
string frameNumber = packet.SelectSingleNode("//field[@name='frame.number']").
Attributes["show"].Value;
Console.WriteLine(frameNumber);
}
If I Debug through the code, it always selects the right Nodes with the correct attributes. But at each iteration there is a "1" printed out.
Does anyone suspect what failure this is? I didn't found anything on the internet for this failure
Thank you very much!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
SelectSingleNode
中的 XPath 以//
开头 - 这意味着“从文档的根目录开始”。因此,你总是获得第一个。只需将该方法中的 XPath 更改为
proto/field[@name='frame.number']
即可。Its because your XPath in
SelectSingleNode
starts with//
- which means "start from the root of the document". Therefore you're always getting the first one.Just change the XPath in that method to
proto/field[@name='frame.number']
.