I have a relativly complex xml message I am recieving from a partner company. I need to parse up the xml to use it. I had it working fine, then they changed the message all around and now none of my SelectSingleNode commands work.
Structure of the message. Note the CDATA object with an embedded xml document. Not sure this matters or not. I was able to tweeze out the dataContent object, so it shouldn't make a difference. Here is what I am loading into the XmlDocument:
<ns5:dataContent xmlns:ns2="http://test/common/v1"
xmlns="http://test/schema/common"
xmlns:ns4="http://test/credentialing/stuff/v1"
xmlns:ns3="http://test/schema/ims/common/v1"
xmlns:ns5="http://test/schema/v1">
<createdBy>Micky</createdBy>
<createdAt>2011-03-08T17:00:27.050-05:00</createdAt>
<ns5:Id>39</ns5:Id>
<ns5:Type>4</ns5:Type>
-- lots more data --
</ns5:dataContent>
my code is fairly straight forward
var xmlDoc = new XmlDocument();
xmlDoc.Load(new StringReader(CDATA content));
var xmlNsM = new XmlNamespaceManager(xmlData.NameTable);
xmlNsM.AddNamespace(String.Empty, @"http://test/schema/common\");
xmlNsM.AddNamespace("ns5", @"http://test/schema/v1\");
xmlNsM.AddNamespace("m", @"http://test/message/v1\");
//This works
var order = xmlDoc.ChildNodes[0];
//This returns null
var ID = order.SelectSingleNode("ns5:Id", xmlNsM);
I can see that xmlDoc is being loaded properly. I have tried adding all 6 namespaces to the xmlNsM, but get the same results. None of my SelectSingleNodes works now.
Every post I have found on this subject says to just use the namespace manager, but I already knew that so its not been a very productive afternoon.
Just reading the code I think you are using the Namespace Manager correctly. Your problem is the XPath query. "ns5:Id" is not a child of the high level document and ChildNodes will only return one level down. You need to be searching decedents. Try:
var root = doc.DocumentElement;
var ID = root.SelectSingleNode("//ns5:Id", xmlNsM);
This isn't the most efficient because it will find all matches in the entire document. Take a look at some of the examples here: http://msdn.microsoft.com/en-us/library/ms256086.aspx