为什么 SelectSingleNode 返回 null?

发布于 2024-07-18 15:01:31 字数 608 浏览 2 评论 0原文

我正在处理一个 XML 文档,其中包含与此类似的结构:

<MT>
  <Events>
    <event id="1">
      <field name="blah" value="a_value" type="atype" />
      .
      .
      .
     </event>
   </Events>
 </MT>

我当前正在以这种方式将其从文件加载到 XML 文档中:

XmlDocument xdoc = new XmlDocument();
xdoc.Load("somefile.xml");  //Successfully loads btw

但是,我遇到了问题,并且仅使用此一个特定文档当我尝试运行下一行代码时:

xdoc.SelectSingleNode("//event[@id='1']"); //This returns a null 

我猜测由于使用名为“id”的属性出现问题而返回 null,我是否走在正确的轨道上,或者我是否在代码中遗漏了某些内容?

I'm working with an XML document that contains a structure that looks similar to this:

<MT>
  <Events>
    <event id="1">
      <field name="blah" value="a_value" type="atype" />
      .
      .
      .
     </event>
   </Events>
 </MT>

I'm currently loading this from a file into an XML document in this fashion:

XmlDocument xdoc = new XmlDocument();
xdoc.Load("somefile.xml");  //Successfully loads btw

However I'm running into a problem and only with this one particular document when I try to run the next line of code:

xdoc.SelectSingleNode("//event[@id='1']"); //This returns a null 

Am I on the right track by guessing that this is returning null because of an issue with using an attribute named 'id' or am I missing something in code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

_畞蕅 2024-07-25 15:01:31

我无法使用 XML 文件复制此内容

<MT>
  <Events>
    <event id="1">
      <field name="blah" value="a_value" type="atype" />
     </event>
   </Events>
</MT>

并且代码

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\test.xml");

XmlNode node = doc.SelectSingleNode("//event[@id='1']");

这会按预期返回一个非空节点。

更新

xmlns="example.org" 添加到 元素后,我必须为 XPath 配置命名空间管理器并使用事件的命名空间。 由于某种原因无法使默认命名空间正常工作。

XmlDocument doc = new XmlDocument();
doc.Load(@"D:\test.xml");

XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("e", "http://example.org");

XmlNode node = doc.SelectSingleNode("//e:event[@id='1']", manager);

当我试图让它发挥作用时,有一件事让我感到困惑。 如果不是为了找出它包含哪些命名空间,为什么 XmlNamespaceManager 需要文档中的 XmlNameTable? 例如,为什么我需要定义 NameTable 和命名空间? 如果有知道的人可以发表简短的评论,我将不胜感激。

I cannot replicate this using an XML file

<MT>
  <Events>
    <event id="1">
      <field name="blah" value="a_value" type="atype" />
     </event>
   </Events>
</MT>

And code

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\test.xml");

XmlNode node = doc.SelectSingleNode("//event[@id='1']");

This returns a non-null node as expected.

Update

After adding a xmlns="example.org" to the <MT> element, I had to configure a namespace manager for the XPath and use the namespace for the event. Couldn't get the default namespace to work for some reason.

XmlDocument doc = new XmlDocument();
doc.Load(@"D:\test.xml");

XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("e", "http://example.org");

XmlNode node = doc.SelectSingleNode("//e:event[@id='1']", manager);

One thing confused me when trying to get this to work. Why does XmlNamespaceManager need XmlNameTable from the document if not for finding out what namespaces it contains? As in, why do I need to define the NameTable and the namespace? I'd appreciate if someone who knows could drop a short comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文