0

I've got this 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:

           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.

EDIT: Current XML SelectNode code updated with NameSpace managing.

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);
Pablo Costa
  • 125
  • 4
  • 14

1 Answers1

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">.

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);
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • I tought your answer was the correct one. But it throws me an exception: Object reference not set to an instance of an object. – Pablo Costa Feb 27 '16 at 16:05
  • 1
    @PabloCosta I think that `parent` is null. Perhaps the XPath is wrong. – Eugene Podskal Feb 27 '16 at 16:06
  • I've checked and it seems to be right. The only thing i haven't counted is the xml .... > on the root. – Pablo Costa Feb 27 '16 at 16:09
  • 1
    @PabloCosta Your xpath is correct (though I would add '/' at the beginning). The problem is with namespace - http://stackoverflow.com/questions/809496/why-is-selectsinglenode-returning-null. Just remove it (xmlns="http://www.CIP4.org/JDFSchema_1_1") and everything will work. Or take a look at http://stackoverflow.com/questions/4171451/xmldocument-selectsinglenode-and-xmlnamespace-issue – Eugene Podskal Feb 27 '16 at 16:21
  • XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("CIP4NS", "http://www.CIP4.org/JDFSchema_1_1"); var parent = xmlDoc.SelectSingleNode("//CIP4NS:JMF/Command/ResourceCmdParams/InkZoneProfile/InkZoneProfile/InkZoneProfile/InkZoneProfile", nsmgr); XmlElement IZP = xmlDoc.CreateElement("InkZoneProfile"); - still returning null. I'm debugging here and reading to analyze what may be wrong. – Pablo Costa Feb 27 '16 at 21:58
  • @PabloCosta Perhaps you can try XDocument. I hope it doesn't have such problems. – Eugene Podskal Feb 27 '16 at 22:14
  • I don't think changing the approach will solve things quicker. I'm one step close to finally write the XML the way i want and move on the the 2nd part of my project. I'm reading some documentations here , but , even if i FOLLOW them it isn't working ;p i'll update the post with the current code – Pablo Costa Feb 27 '16 at 22:28
  • @PabloCosta I would actually recommend you to create a new question that will deal specifically with this issue. It will help someone in the future more than now it being a part of a big discussion and minor fixes. – Eugene Podskal Feb 27 '16 at 22:51
  • 1
    i just did it :) - http://stackoverflow.com/questions/35676471/selectsinglenode-returns-null-even-with-namespace-managing – Pablo Costa Feb 27 '16 at 22:53