【问题标题】:SelectSingleNode returns null even with namespace managing即使使用命名空间管理,SelectSingleNode 也会返回 null
【发布时间】:2016-06-11 03:06:50
【问题描述】:

我来自这个问题,我的第一个问题在这里得到了解决: XML Select a single node where the names are repeating 首先是命名空间问题。

但现在即使使用 corect NameSpace 管理我的 XPath 仍然返回 null。

我也检查过:

SelectSingleNode return null - even with namespace SelectSingleNode always returns null? XmlDocument.SelectSingleNode and xmlNamespace issue SelectSingleNode returning null for known good xml node path using XPath Why is SelectSingleNode returning null?

但他们都没有帮助我。我在这个问题上被困了几个小时。它有什么问题?

感谢您的帮助。

示例 XML(编辑:完整 XML)

<?xml version="1.0" encoding="utf-8"?>
<JMF SenderID="InkZone-Controller" Version="1.2" xmlns="http://www.CIP4.org/JDFSchema_1_1">
    <Command ID="cmd.00695" Type="Resource">
        <ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
            <InkZoneProfile ID="r0013" Class="Parameter" Locked="false" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
                <InkZoneProfile SignatureName="SIG1">
                    <InkZoneProfile Locked="false" SheetName="S1">
                        <InkZoneProfile Side="Front">
                            <InkZoneProfile Separation="designer P&G 1901" ZoneSettingsX="0.391 "/>

                        </InkZoneProfile>
                    </InkZoneProfile>
                </InkZoneProfile>
            </InkZoneProfile>
        </ResourceCmdParams>
    </Command>
</JMF>

我选择指定 XML 节点的代码:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\XML\\test.xml");
XmlNode root = xmlDoc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("CIP4NS", "http://www.CIP4.org/JDFSchema_1_1");

var parent = root.SelectSingleNode("//CIP4NS:Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile", nsmgr);
XmlElement IZP = xmlDoc.CreateElement("InkZoneProfile");
IZP.SetAttribute("Separation", x.colorname);
IZP.SetAttribute("ZoneSettingsX", x.colorvalues);
parent.AppendChild(IZP);
xmlDoc.Save("C:\\XML\\test.xml");

【问题讨论】:

  • 我还建议您使 xml 独立(在当前形式下,它不是完整的文档)。所以可以复制。并将您的代码重新制作成stackoverflow.com/help/mcve。这样它就不会依赖 GlobalVars 并且可以尽可能容易地复制和测试。
  • 我用 GlobalVars 编辑了这些部分。它可以在其他任何地方复制和使用。

标签: c# xml xpath xmldocument default-namespace


【解决方案1】:

您的 XML 具有 默认命名空间,没有前缀的后代元素隐式继承祖先。这意味着,不仅根元素,而且 XPath 中提到的所有元素都在同一个默认命名空间中,因此需要使用相同的前缀来引用:

//CIP4NS:Command/CIP4NS:ResourceCmdParams/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile

【讨论】:

  • 仍然为空。正如它可能看到的那样,我创建了一个 var 来保存根。我正在做 root.SelectSingleNode (root 将我返回为 JMF)。我也应该在 Xpath 中包含 JMF 吗?
  • 刚试过:var parent = root.SelectSingleNode("descendant::CIP4NS:Command/CIP4NS:ResourceCmdParams/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile", nsmgr); (因为 MSDN 有一些关于包括后代的东西) - 但也没有结果。
  • // 在这种情况下的效果与使用 descendant 轴相同,因此不会改变任何内容。无论如何,刚刚创建了一个演示,XPath 在那里运行良好:dotnetfiddle.net/vJ8h9S
  • 在这里也工作得很好。发生了一些非常奇怪的事情。我还检查了持有 xml 路径的 var 及其罚款。检查是否正在创建 XML 并且是。在获得 SingleNode 之前一切正常。我想我得调试一下才能知道到底发生了什么。
  • 调试时我发现 root.InnerXml 本身已经加载了内容(与 XmlDoc.InnerXml 相同)。但是 InnerXml 没有实现 SelectSingleNode 的方法。
猜你喜欢
  • 2023-03-07
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多