[英]XML Select a single node where the names are repeating
I've got this 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 0.36 0.097 0.058 0 0 0 0 0 0 0 0 0.178 0.394 0.201 0.088"/>
I'm trying to append elements just after the node but i'm not being able to. 我正在尝试在节点之后追加元素,但我无法这样做。 With my code i've tried to select the node with XPath:
用我的代码,我试图用XPath选择节点:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(GlobalVars.FullPath);
xmlDoc.SelectSingleNode("JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile");
XmlElement IZP = xmlDoc.CreateElement("InkZoneProfile");
IZP.SetAttribute("Separation", x.colorname);
IZP.SetAttribute("ZoneSettingsX", x.colorvalues);
xmlDoc.DocumentElement.AppendChild(IZP);
xmlDoc.Save(GlobalVars.FullPath);
But it isn't appending to the node i've selected - instead, it keeps appending to the last line. 但是它没有追加到我选择的节点上,而是一直追加到最后一行。 How can i append to this specific position ?
我该如何补充这个特定职位? Am i missing some argument ?
我想念一些论点吗?
Thanks. 谢谢。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(GlobalVars.FullPath);
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(GlobalVars.FullPath);
xmlDoc.SelectSingleNode("JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile");
xmlDoc.SelectSingleNode("JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile");
的返回值xmlDoc.SelectSingleNode("JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile");
. XmlDocument
- it returns an XmlNode
under the specified path. XmlDocument
任何内容-它会在指定路径下返回一个XmlNode
。 xmlDoc.DocumentElement.AppendChild
will append the element just before the end of the root element and your root element is <JMF SenderID="InkZone-Controller" Version="1.2" xmlns="http://www.CIP4.org/JDFSchema_1_1">
. xmlDoc.DocumentElement.AppendChild
将在根元素末尾之前追加元素,并且您的根元素为<JMF SenderID="InkZone-Controller" Version="1.2" xmlns="http://www.CIP4.org/JDFSchema_1_1">
。 So you should probably save the result of the SelectSingleNode
and append the child to it: 因此,您可能应该保存
SelectSingleNode
的结果并将其子代附加到它:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(GlobalVars.FullPath);
var parent = xmlDoc.SelectSingleNode("JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile");
XmlElement IZP = xmlDoc.CreateElement("InkZoneProfile");
IZP.SetAttribute("Separation", x.colorname);
IZP.SetAttribute("ZoneSettingsX", x.colorvalues);
parent.AppendChild(IZP);
xmlDoc.Save(GlobalVars.FullPath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.