0

I want to retrieve the currency value from National Bank XML. The XML contains the value for EUR for 10 days. I want to take the EUR value from the previous date.

For example if today is 2016-03-23 I want the EUR value from 2016-03-22.

I am using XPath for Parsing the XML. How can I verify an attribute using a string ?

First I tried parsing XML with XPathExpression .

XmlDocument XML_De_La_BNR = new XmlDocument();
XML_De_La_BNR.LoadXml(sbXmlText.ToString());

XPathNavigator Navigator_Prin_XML = XML_De_La_BNR.CreateNavigator();

XmlNamespaceManager Manager_Spatii_De_Nume = new XmlNamespaceManager(Navigator_Prin_XML.NameTable);
Manager_Spatii_De_Nume.AddNamespace("BNR", "http://www.bnr.ro/xsd");  

string date_yesterday = DateTime.Now.AddDays(-1).Date.ToString("yyyy-MM-dd");       

XPathExpression Interogare_XPath_Date = Navigator_Prin_XML.Compile("/BNR:DataSet/BNR:Body/BNR:Cube[@date='date_yesterday']/BNR:Rate[@currency='EUR']");
Interogare_XPath_Date.SetContext(Manager_Spatii_De_Nume);
XPathNavigator Nod_Rata_Date = Navigator_Prin_XML.SelectSingleNode(Interogare_XPath_Date);  

Second method , I tried with a foreach where I look for every 'Cube' note and retrieve a date. And after I will have an if condition if that date is equal with mydate retrieve EUR value.

List<string> dates = new List<string>();
foreach (XmlNode node in XML_De_La_BNR.GetElementsByTagName("Cube"))
{
    XmlNode date_y = node["date"];
    dates.Add(date_y.InnerText);
}
Adnan Umer
  • 3,669
  • 2
  • 18
  • 38
mihral
  • 323
  • 6
  • 19
  • 1
    That's a *lot* of text here, much of which seems irrelevant to your problem. Please reduce this to a [mcve] - it will make it much easier to help you. (Aslo, do you definitely *have* to use XPath and XmlDocument? Any reason you don't want to use LINQ to XML to do the querying? I suspect it would be significantly simpler.) – Jon Skeet Mar 23 '16 at 14:14
  • I've closed this as a dupe of your better-posed question from 24 hours later. In future, please don't post multiple versions of the same question; instead, you should **edit** questions to improve them. – AakashM Mar 24 '16 at 12:21

0 Answers0