1

I've come from this question, where my first issue was solved: XML Select a single node where the names are repeating It was a namespace issue first.

But now even with the corect NameSpace managing my XPath still returns me null.

I've also checked:

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?

But none of 'em helped me. I'm stuck for some hours on this issue. What is wrong with it ?

Thanks for any help.

Sample XML(EDITED: FULL 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>

My code for selecting the specified XML Node:

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");
Community
  • 1
  • 1
Pablo Costa
  • 125
  • 4
  • 14
  • 1
    I also recommend you to make the xml self-contained (in its current form it is not a full document). So it can be copied. And remake your code into http://stackoverflow.com/help/mcve. So that it won't depend on GlobalVars and can be copied and tested as easily as possible. – Eugene Podskal Feb 27 '16 at 22:56
  • I edited the parts with GlobalVars. It can be copied and used erverywhere else. – Pablo Costa Feb 27 '16 at 23:07

1 Answers1

3

Your XML has default namespace which descendant elements without prefix implicitly inherits from the ancestor. That implies, not only the root element but all elements mentioned in your XPath are in the same default namespace, hence need to be referenced by using the same prefix :

//CIP4NS:Command/CIP4NS:ResourceCmdParams/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile
har07
  • 88,338
  • 12
  • 84
  • 137
  • Still null. As its possible to see i've created a var to hold the root. And i'm doing root.SelectSingleNode (root returns me as JMF). Should i include JMF in the Xpath also ? – Pablo Costa Feb 27 '16 at 23:01
  • Just tried: var parent = root.SelectSingleNode("descendant::CIP4NS:Command/CIP4NS:ResourceCmdParams/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile/CIP4NS:InkZoneProfile", nsmgr); (because MSDN has something about including descendant) - but no result also. – Pablo Costa Feb 27 '16 at 23:11
  • `//` effect in this case is the same as using `descendant` axis, so that will not change anything. Anyway, just created a demo and the XPath worked fine there : https://dotnetfiddle.net/vJ8h9S – har07 Feb 27 '16 at 23:20
  • Worked fine here too. There's something really weird going on. I've also checked the var who holds the xml path and its fine. Checked if the XML was being created and it is. Everything is working fine till the point of obtaining the SingleNode. Guess i'll have to debug to find out what the heck is going on. – Pablo Costa Feb 27 '16 at 23:38
  • When debugging i've found that root.InnerXml has the contents loaded on itself (same as XmlDoc.InnerXml ). But InnerXml doesn't implement a method to SelectSingleNode. – Pablo Costa Feb 27 '16 at 23:46