Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an XML like this
XML
<Calendar region="abc">
  <Format version="1.11"/>
  <Data version="0"/>
  <Mode mode="FULL"/>
  <Period end="31122019" start="15122013"/>
  <Holidays>
    <Day serviceday="2" date="15122013"/>
  </Holidays>
  <ListOfRday>
    <Rday number="11">
      <Description text="Mon"/>
      <AppointedPeriod end="31122019" start="15122013" servicedays="11"/>
    </Rday>
  </ListOfRday>
</Calendar>

There are many Rday entries in ListOfRday.
How can I get the description text for a particular R-day, say Rday number="20", using LINQ?
Posted
Updated 20-Mar-14 11:54am
v2

1 solution

Here is a code snippet for you:
C#
using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Your XML here
            string xml = "<Calendar/>";
            XElement calendar = XElement.Parse(xml);
            var query = from rday in calendar.Descendants("Rday")
                        where rday.Attribute("number").Value == "20"
                        select (string)rday.Element("Description").Attribute("text");
            foreach (var text in query)
            {
                Console.WriteLine(text);
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900