Selecting XML nodes defined with namespaces

While isolating nodes with custom namespace definition, it might be problematic to retrieve the nodes using XPath (SelectSingleNode or SelectNodes methods). Because if the target XML document contains a URN prefix in the node, that has to be defined and explicitly and specified in the SelectSingleNode call. Here is the way to get it into work.

XmlNamespaceManager nsmgr=null; //Declare Namespace manager to track all URNs and prefixes used in target document.

nsmgr=new XmlNamespaceManager(xdoc.NameTable); //xdoc contains the returned SOAP envelope if successful

nsmgr.AddNamespace("prfx", ""); //add entry about the custom prefix

nsmgr.AddNamespace("soap", ""); //add entry about default soap prefix available in the SOAP envelope.

catch(NullReferenceException nex){Console.WriteLine(nex.Message);} //In case the XML doc wasn’t accessible

XmlNode result = xdoc.SelectSingleNode("//prfx:Operation",nsmgr); //result will now contain the prfx:Operation element or attribute as the case may be.