This a simplified version of a more complex XML
that I need to manage with C#
.
The problem is that when I try to access to a tag within a namespace, the XPATH
does not work.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<s:Body xmlns:s=\"sssssss\"><s:SessionID>abcde</s:SessionID></s:Body>");
string xpath = "//*[local-name()='s:SessionID']";
Context.UserLogger.Info(xmlDoc.SelectSingleNode(xpath).InnerText);
//Object reference not set to an instance of an object
But the code works perfect without the colon on the tag.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<s:Body xmlns:s=\"sssssss\"><SessionID>abcde</SessionID></s:Body>");
string xpath = "//*[local-name()='SessionID']";
Context.UserLogger.Info(xmlDoc.SelectSingleNode(xpath).InnerText);
//abcde
I have ensured on a XPATH validator that the "//*[local-name()='s:SessionID']" works fine.
What I am missing?
Thanks in advance,
I have read info about XmlNamespaceManager
but I would prefer to use "direct" paths. The XML is full of NameSpaces and it is dynamic, so its the structure changes quite often.