XML 选择名称重复的单个节点 [英] XML Select a single node where the names are repeating

查看:34
本文介绍了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"/>

我试图在节点之后追加元素,但我无法做到.使用我的代码,我尝试使用 XPath 选择节点:

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:

           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 ?

谢谢.

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);

推荐答案

  1. 您不使用 xmlDoc.SelectSingleNode("JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile"); 的返回值.XmlNode.SelectSingleNode 不会改变任何东西在 XmlDocument 中 - 它返回指定路径下的 XmlNode.
  2. xmlDoc.DocumentElement.AppendChild 将在 根元素的结尾 并且您的根元素是 <JMF SenderID="InkZone-Controller" Version="1.2" xmlns=http://www.CIP4.org/JDFSchema_1_1">.
  1. You do not use the return value of the xmlDoc.SelectSingleNode("JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile");. XmlNode.SelectSingleNode doesn't change anything in the XmlDocument - it returns an XmlNode under the specified path.
  2. 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">.

所以你可能应该保存 SelectSingleNode 的结果并将子节点附加到它:

So you should probably save the result of the SelectSingleNode and append the child to it:

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);

这篇关于XML 选择名称重复的单个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆