mihral
mihral

Reputation: 353

Linq to XML conditioning attribute

This is my xml

<DataSet xmlns="http://www.bnr.ro/xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd">
    <Header>
        <Publisher>National Bank of Romania</Publisher>
        <PublishingDate>2016-03-24</PublishingDate>
    <MessageType>DR</MessageType>
    </Header>
    <Body>
        <Subject>Reference rates</Subject>
        <OrigCurrency>RON</OrigCurrency>
        <Cube date="2016-03-24">            
            <Rate currency="EUR">4.4655</Rate>          
        </Cube>
        <Cube date="2016-03-23">                
            Rate currency="EUR">4.4641</Rate>               
        </Cube>
    </Body>
</DataSet>

I want to verify the Cube Attribute date to receive the EUR value from yesterday date.

For example if today is 2016-03-24 I want to receive the value 4.4641 from 2016-03-23.

I tried with LINQ to XML

    string date_yesterday = DateTime.Now.AddDays(-1).Date.ToString("yyyy-MM-dd");
XElement root = XElement.Parse(sbXmlText.ToString());
                IEnumerable<XElement> adress =
                    from el in root.Descendants("Cube")
                    let z = el.ElementsAfterSelf().FirstOrDefault()
                    where z != null && (string)el.Attribute("date") == date_yesterday
                    select el;
                foreach (XElement el in adress)
                    Console.WriteLine(el);

And tried

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

             XElement root = XElement.Parse(sbXmlText.ToString());
             IEnumerable<XElement> adress =
                 root.Descendants("Cube").Where(r => r.Attribute("date").Value == date_yesterday);                    
             foreach (XElement el in adress)
                 Console.WriteLine(el);

And it returns everytime null

Upvotes: 2

Views: 166

Answers (1)

har07
har07

Reputation: 89285

Your XML has default namespace. You can use "XNamespace+element's local-name" to reference element in namespace, for example :

var xml = @"<DataSet xmlns='http://www.bnr.ro/xsd' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:schemaLocation='http://www.bnr.ro/xsd nbrfxrates.xsd'>
    <Header>
        <Publisher>National Bank of Romania</Publisher>
        <PublishingDate>2016-03-24</PublishingDate>
    <MessageType>DR</MessageType>
    </Header>
    <Body>
        <Subject>Reference rates</Subject>
        <OrigCurrency>RON</OrigCurrency>
        <Cube date='2016-03-24'>            
            <Rate currency='IDR'>1.1111</Rate>
            <Rate currency='EUR'>4.4655</Rate>          
        </Cube>
        <Cube date='2016-03-23'>                
            <Rate currency='EUR'>4.4641</Rate>               
        </Cube>
    </Body>
</DataSet>";
var doc = XDocument.Parse(xml);

//XNamespace that reference default namespace URI:
XNamespace d = "http://www.bnr.ro/xsd";

var yesterday = DateTime.Now.AddDays(-1).Date;

//Use `XNamespace+element's local-name` to reference element in namespace:
var result = (from cube in doc.Descendants(d+"Cube")
              from rate in cube.Elements(d+"Rate")
              where 
                ((DateTime)cube.Attribute("date")).Date == yesterday
                    && 
                (string)rate.Attribute("currency") == "EUR"
              select (decimal)rate
              ).FirstOrDefault();
Console.WriteLine(result);

output :

4.4641

Upvotes: 2

Related Questions