如何使用 C# 读取没有关联前缀的命名空间的 XML 文档

发布于 2024-12-11 21:11:11 字数 827 浏览 0 评论 0原文

我正在尝试阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舂唻埖巳落 2024-12-18 21:11:11

您的 XPath 表达式缺少命名空间前缀。

您尝试选择的元素的命名空间 URI 为 http://www.bibletechnologies.net/2003/OSIS/namespace,并且 XPath 不会匹配这些使用带有空命名空间 URI 的路径的节点。

我在 .NET 2.0 中测试了这个版本,它找到了预期的节点。

XPathDocument document = new XPathDocument("osis.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager xmlns = new XmlNamespaceManager(navigator.NameTable);
xmlns.AddNamespace("osis", "http://www.bibletechnologies.net/2003/OSIS/namespace");

XPathNodeIterator nodes = navigator.Select("/osis:osis/osis:osisText", xmlns);

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.

XPathDocument document = new XPathDocument("osis.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager xmlns = new XmlNamespaceManager(navigator.NameTable);
xmlns.AddNamespace("osis", "http://www.bibletechnologies.net/2003/OSIS/namespace");

XPathNodeIterator nodes = navigator.Select("/osis:osis/osis:osisText", xmlns);
天冷不及心凉 2024-12-18 21:11:11

您可以将文件读取为字符串,替换内存中的命名空间,然后使用字符串流加载它:

string s;
using(var reader = File.OpenText("osis.xml"))
{
    s = reader.ReadToEnd();
}
s = s.Replace("xmlns=\"http://www.bibletechnologies.net/2003/OSIS/namespace\"", "");
Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(s));
XPathDocument document = new XPathDocument("stream");
// Rest of the code

You can read the file to a string, replace the namespace in memory, and then load it using a string stream:

string s;
using(var reader = File.OpenText("osis.xml"))
{
    s = reader.ReadToEnd();
}
s = s.Replace("xmlns=\"http://www.bibletechnologies.net/2003/OSIS/namespace\"", "");
Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(s));
XPathDocument document = new XPathDocument("stream");
// Rest of the code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文