Regarding a query in LINQ to XML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanndeb
    New Member
    • May 2010
    • 33

    Regarding a query in LINQ to XML

    Hi, Hope everybody is file here....

    I have an xml like this
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <Holidays>
    	<year year="2010">
                <holiday day="03/01/2010" />
                <holiday day="03/02/2010" />
            </year>
            <year year="2011">
                <holiday day="03/01/2011" />
                <holiday day="03/02/2011" />
                <holiday day="03/03/2011" />
            </year>
    </Holidays>
    Now I want an string array of holidays for year 2010
    I'm trying something like
    Code:
    XDocument holidays = XDocument.Load("path to xml/ xmlreader");
    var days = from el in holidays.Root.Elements("year")
                where el.Attribute("year").Value == "2010"
                 select el.Elements("holiday");
    foreach (var date in days)
    {
    // But here date is not what i want like '03/01/2010' or '03/02/2010'
    }
    Can you please suggest how to extract the holidays for year 2010 from the xml.

    Thanks :)

    Edit: And also how do i do the same when the xml is of the form
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Holidays>
      <Year Year="2010">
        <Holiday>"03/01/2010"</Holiday>
        <Holiday>"05/27/2010"</Holiday>
      </Year>
      <Year Year="2011">
        <Holiday>"03/01/2011"</Holiday>
        <Holiday>"05/27/2011"</Holiday>
      </Year>
    
    </Holidays>
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    date is of type XElement (holiday), you've to extract it's day-attribute and read it's value.
    e.g.
    date.Attributes ("day").First() .Value;

    In your second example, the date is the value of your date-XElement, so you'd just have to do date.Value within you foreach-loop.

    Comment

    • sanndeb
      New Member
      • May 2010
      • 33

      #3
      Thats ok to get the values using date.Attributes ("day").First() .Value inside the foreach loop... but what i wanted is to extract the dates directly using linq not inside another foreach loop.... i.e like at the end of the linq query i'll add a toList() method so that all day's will be listed in a list of string.

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        Sure you can do this
        Code:
        var days = from el in holidays.Root.Elements("year")
                   where el.Attribute("year").Value == "2010"
                   let holidayElem = el.Elements("holiday")
                   let attrib = holidayElem.Attributes("day").FirstOrDefault()
                   where attrib != null
                   select attrib.Value;
        Last edited by Christian Binder; Aug 20 '10, 10:14 AM. Reason: code tags

        Comment

        • sanndeb
          New Member
          • May 2010
          • 33

          #5
          But your query is returning only the first day of year 2010 i.e '03/01/2010', but the output should be of two days...'03/01/2010' & '03/02/2010'

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            Oh, I'm sorry.

            Code:
            var days = from el in holidays.Root.Elements("year")
                       where el.Attribute("year").Value == "2010"
                       from holidayElem in el.Elements("holiday")
                       let attrib = holidayElem.Attributes("day").FirstOrDefault()
                       where attrib != null
                       select attrib.Value;

            Comment

            • sanndeb
              New Member
              • May 2010
              • 33

              #7
              Yes thats it mate... getting the results perfectly... thanks a lot :)

              Comment

              Working...