C#:“瑞典语” 解析 Lating1Encoded 文档时 Xpath 中的字符
我有一组需要解析的 html 文档。 它们以 Latin1Encoded 编码。 我正在使用 HtmlAgiliy 包进行“解析”。
我有一个 Xpath 查询(带有瑞典字符),由于文档之间的编码不同以及编码 VS 将 XPath 查询存储在其中,所以我无法工作。
Xpath 查询:
doc.DocumentNode.SelectNodes(@"//h2[text()='Företag']/../div//span[text()='Resultat:']/../div");
xpath 查询在 Firefox 扩展 xpath 检查器中工作正常。
I've a set of html docs that I need to parse. They are encoded in Latin1Encoded. I'm using HtmlAgiliy pack for "parsing".
I have a Xpath query (with swedish characters) that I can't get to work because of different encodings between the docs and the encoding VS stores the XPath query in??
Xpath query:
doc.DocumentNode.SelectNodes(@"//h2[text()='Företag']/../div//span[text()='Resultat:']/../div");
The xpath query works fine in the Firefox extension xpath checker.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否提供更多示例代码和一些输入 XML 文档? 根据给出的信息,我编写了一个小示例程序,它按预期工作。 以下内容对您有用吗?
示例文档:
C#
输出
从给出的示例代码来看,您似乎正在使用
Microsoft.Windows.Design.Documents.Trees.DocumentNode
类。 但是,文档指出该类不适合直接使用。 我可以问一下你想做什么吗?更新:您可能面临空白规范化问题(这可能是由您的 FireFox 加载项完成的,而不是在您的代码中完成的)。 您是否尝试通过将测试
text() = 'Företag'
替换为normalize-space() = 'Företag'
来更改 XPath(只是为了排除存在以下情况的情况)是额外的前导或尾随空格)?Could you provide more sample code and some input XML document? From the information given I wrote a little sample program which just works as expected. Does the following work for you?
Sample document:
C#
Output
From the sample code given it seems that you are using the
Microsoft.Windows.Design.Documents.Trees.DocumentNode
class. However, the documentation states that this class is not intended to be used directly. May I ask what you are trying to do?Update: It might be that you are facing an issue with whitespace normalization (which might be done by your FireFox add-in and not in your code). Have you tried to change your XPath by replacing the test
text() = 'Företag'
bynormalize-space() = 'Företag'
(Just to exclude the case that there is additional leading or trailing whitespace)?