1

I need to select a node which has attribute name as _1.1.1

I am trying to select the node as

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "http://schemas.microsoft.com/wix/2006/objects");    
XmlNode xRootNode = xmlDoc.SelectSingleNode("//folder[@name='Global']");

But it doesn't return any. I'm sure its because of the special characters in my expression. How should I handle it to get the desired node?

EDIT: I access node as

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "http://schemas.microsoft.com/wix/2006/objects");    
XmlNode xRootNode = xmlDoc.SelectSingleNode("//folder[@name='Global']", nsmgr);

and the XML is

<?xml version="1.0" encoding="UTF-8"?>
<workplace xmlns='IKS:'>
  <cabinet name='Groups%20and%20Departments' oid='_1.25.18'>
    <folder name='Global' oid='_1.11.9882'></folder>
  </cabinet>
</workplace>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
Saksham
  • 9,037
  • 7
  • 45
  • 73
  • No, there's nothing problematic or special about the value `_1.1.1`. If you remove the `[@name='_1.1.1']` part from the XPath, does it successfully select anything? Can you show us your XML? – JLRishe Dec 10 '14 at 09:43
  • 2
    My crystal ball tells me that your XML has a namespace defined and you did not honor that. – Tomalak Dec 10 '14 at 10:11
  • @Tomalak I was looking at that only but it is still not helping me. – Saksham Dec 10 '14 at 10:12
  • @Tomalak childnodes is getting me the nodes, but selectsinglenode isnt. – Saksham Dec 10 '14 at 10:14
  • Look for `xmlns` declarations in your XML and then search for `XMLNamespaceManager` here on SO. For example [this answer](http://stackoverflow.com/questions/4271689/), one of literally more than a thousand that all point out the same thing. – Tomalak Dec 10 '14 at 10:16
  • @Saksham Is there a reason you have ignored my plea to see your XML? – JLRishe Dec 10 '14 at 10:23
  • possible duplicate of [XPath not working correctly with XFA](http://stackoverflow.com/questions/14460688/xpath-not-working-correctly-with-xfa) – JLRishe Dec 10 '14 at 10:24

1 Answers1

4

You're very close to having the correct approach. You have declared a namespace prefix, but you need to actually use it:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "http://schemas.microsoft.com/wix/2006/objects");    
XmlNode xRootNode = xmlDoc.SelectSingleNode("//IKS:folder[@name='Global']");
//                                             ^^^^------- here

Note: For some reason, you have xmlns="IKS:" in your XML. If that is in fact what your XML looks like, then IKS: is the namespace URI you need to use:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("IKS", "IKS:");    
XmlNode xRootNode = xmlDoc.SelectSingleNode("//IKS:folder[@name='Global']");
JLRishe
  • 99,490
  • 19
  • 131
  • 169