Generic XML XPath helper with C#

464 Views Asked by At

Problem- I have a windows application which loads XML file and as other input takes XPath to fetch data which user wants from given XML (obviously that's not a problem). Code snippet I use to load element from given XPath is like-

XPathDocument docNav;
XPathNavigator nav;
XPathNodeIterator NodeIter;
XmlNamespaceManager manager;

docNav = new XPathDocument(fileName);
nav = docNav.CreateNavigator();

nav.MoveToFollowing(XPathNodeType.Element);
IDictionary<string, string> names = nav.GetNamespacesInScope(XmlNamespaceScope.All);

manager = new XmlNamespaceManager(nav.NameTable);

foreach (KeyValuePair<string, string> item in names)
{
    manager.AddNamespace(item.Key, item.Value);
}

NodeIter = nav.Select(path, manager);

I have one sample XML as below-

<FpML xmlns:ns="http://www.fpml.org/2005/FpML-4-2">
  <header>
    <messageId>ts4-XYZ</messageId>
    <sentBy>XYZ</sentBy>
    <creationTimestamp>2017-07-08T08:05:53.929Z</creationTimestamp>
  </header>
  <trade>
    <tradeHeader>
      <partyTradeIdentifier>
        <partyReference href="OUR_PARTY"/>
        <tradeId tradeIdScheme="uniqueId">AAAAAA</tradeId>
      </partyTradeIdentifier>
      <partyTradeInformation>
        <partyReference href="OUR_PARTY"/>
        <trader>dummy trader name</trader>
      </partyTradeInformation>
      <tradeDate>2017-07-08</tradeDate>
      <tsfpml:completedDateTime>2017-07-08T08:05:53.656Z</tsfpml:completedDateTime>
    </tradeHeader>
  </trade>  
</FpML>

Now, I want to retrieve the 'trader' element value. It works if I use XPath query as

//*[local-name()="trader" and namespace-uri()='http://www.fpml.org/2005/FpML-4-2']

But it fails to load any element if I give /FpML/trade/tradeHeader/partyTradeInformation/trader

What is change in my XPath helper application required so as to select nodes with default namespace in general.

Thank you in advance!

2

There are 2 best solutions below

3
On

You can remove namespaces using :

        public static string RemoveAllNamespaces(string xmlDocument)
        {
            XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));

            return xmlDocumentWithoutNs.ToString();
        }
        private static XElement RemoveAllNamespaces(XElement xmlDocument)
        {
            if (!xmlDocument.HasElements)
            {
                XElement xElement = new XElement(xmlDocument.Name.LocalName);
                xElement.Value = xmlDocument.Value;

                foreach (XAttribute attribute in xmlDocument.Attributes())
                    xElement.Add(attribute);

                return xElement;
            }
            return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
        }

And called by :

var doc = new XmlDocument();
doc.Load(fileName);
doc.InnerText = RemoveAllNamespaces(doc.InnerText);
var nav = doc.CreateNavigator();
1
On

Then, you haven't many choice, use XmlNamespaceManager:

var docNav = new XPathDocument("");
var nav = docNav.CreateNavigator();
XmlNamespaceManager nsmanager = new XmlNamespaceManager(docNav.NameTable);
nsmanager.AddNamespace("x", "ns:http://www.fpml.org/2005/FpML-4-2");
var nodes = nav.Select(@"/x:FpML/trade/tradeHeader/partyTradeInformation/trader", nsmanager);

It should work. I can't test myself, but if it doesn't work, test changing Namespace uri without "ns:", or add others "x:" in your XPath and edit my answer with the correct answer ;)