>我来自这个问题,我的第一个问题解决了:XML 选择名称重复的单个节点首先是命名空间问题。
但是现在即使使用corect命名空间管理我的XPath仍然返回我null。
我还检查了:
选择单节点返回空值 - 即使使用命名空间选择单节点总是返回空?XmlDocument.SelectSingleNode 和 xmlNamespace 问题选择单节点,使用 XPath 为已知良好的 xml 节点路径返回空值为什么 SelectSingleNode 返回 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 具有默认命名空间,不带前缀的后代元素从祖先隐式继承。这意味着,不仅根元素,而且XPath中提到的所有元素都位于相同的默认命名空间中,因此需要使用相同的前缀来引用:
//CIP4NS:Command/CIP4NS:ResourceCmdParams/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile