如何使用 C# 读取没有关联前缀的命名空间的 XML 文档
我正在尝试阅读 OSIS 格式的文档。我已将文档缩减为一个简单的片段:
<?xml version="1.0" encoding="utf-8"?>
<osis xmlns="http://www.bibletechnologies.net/2003/OSIS/namespace">
<osisText osisRefWork="Bible" osisIDWork="kjv" xml:lang="en">
</osisText>
</osis>
我尝试使用 MSDN 文档中的示例代码来阅读它:
XPathDocument document = new XPathDocument("osis.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("/osis/osisText");
while (nodes.MoveNext())
{
Console.WriteLine(nodes.Current.Name);
}
问题是选择不包含节点并且不会引发任何异常。由于代码丢弃了根标记,因此我无法阅读该文档。如果我从根 osis 标记中删除 xmlns="http://www.bibletechnologies.net/2003/OSIS/namespace" ,它就可以正常工作。令人反感的 URL 返回 404 代码,但除此之外我认为此 XML 没有任何问题。有人可以解释为什么这段代码无法读取文档吗?除了在尝试加载每个文档之前手动编辑它之外,我还有什么选择?
I am trying to read OSIS formatted documents. I have cut the document down to a simple fragment:
<?xml version="1.0" encoding="utf-8"?>
<osis xmlns="http://www.bibletechnologies.net/2003/OSIS/namespace">
<osisText osisRefWork="Bible" osisIDWork="kjv" xml:lang="en">
</osisText>
</osis>
I try to read it with this sample code from the MSDN documentation:
XPathDocument document = new XPathDocument("osis.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("/osis/osisText");
while (nodes.MoveNext())
{
Console.WriteLine(nodes.Current.Name);
}
The problem is that the selection contains no nodes and throws no exception. Since the code discards the root tag, I can't read the document. If I remove the xmlns="http://www.bibletechnologies.net/2003/OSIS/namespace" from the root osis tag, it works just fine. The offensive URL returns a 404 code, but otherwise I see nothing wrong with this XML. Can someone explain why this code won't read the document? What options do I have besides hand editing every document before trying to load it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 XPath 表达式缺少命名空间前缀。
您尝试选择的元素的命名空间 URI 为
http://www.bibletechnologies.net/2003/OSIS/namespace
,并且 XPath 不会匹配这些使用带有空命名空间 URI 的路径的节点。我在 .NET 2.0 中测试了这个版本,它找到了预期的节点。
Your XPath expression is missing a namespace prefix.
The element that you're trying to select has a namespace URI of
http://www.bibletechnologies.net/2003/OSIS/namespace
, and XPath will not match these nodes using paths with an empty namespace URI.I tested this revision in .NET 2.0 and it found the node as expected.
您可以将文件读取为字符串,替换内存中的命名空间,然后使用字符串流加载它:
You can read the file to a string, replace the namespace in memory, and then load it using a string stream: