【发布时间】: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