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!
You can remove namespaces using :
And called by :