Search code examples
c#linqends-with

Get XML node value using .EndsWith


I have the following XML

<location>
<name>Name</name>
<weeks>
<week>
  <Sunday>
    <times>
      <currently_open>false</currently_open>
      <status>open</status>
      <hours>
        <hour>
          <from>10am</from>
          <to>6pm</to>
        </hour>
      </hours>
    </times>
    <date>2018-09-23</date>
    <rendered>10am - 6pm</rendered>
  </Sunday>
  <Monday>
    <times>
      <currently_open>false</currently_open>
      <status>open</status>
      <hours>
        <hour>
          <from>8:30am</from>
          <to>12am</to>
        </hour>
      </hours>
    </times>
    <date>2018-09-24</date>
    <rendered>8:30am - 12am</rendered>
  </Monday>
  <Tuesday>
    <times>
      <currently_open>false</currently_open>
      <status>open</status>
      <hours>
        <hour>
          <from>8:30am</from>
          <to>12am</to>
        </hour>
      </hours>
    </times>
    <date>2018-09-25</date>
    <rendered>8:30am - 12am</rendered>
  </Tuesday>
  <Wednesday>
    <times>
      <currently_open>true</currently_open>
      <status>open</status>
      <hours>
        <hour>
          <from>8:30am</from>
          <to>12am</to>
        </hour>
      </hours>
    </times>
    <date>2018-09-26</date>
    <rendered>8:30am - 12am</rendered>
  </Wednesday>
  <Thursday>
    <times>
      <currently_open>false</currently_open>
      <status>open</status>
      <hours>
        <hour>
          <from>8:30am</from>
          <to>12am</to>
        </hour>
      </hours>
    </times>
    <date>2018-09-27</date>
    <rendered>8:30am - 12am</rendered>
  </Thursday>
  <Friday>
    <times>
      <currently_open>false</currently_open>
      <status>open</status>
      <hours>
        <hour>
          <from>8:30am</from>
          <to>12am</to>
        </hour>
      </hours>
    </times>
    <date>2018-09-28</date>
    <rendered>8:30am - 12am</rendered>
  </Friday>
  <Saturday>
    <times>
      <currently_open>false</currently_open>
      <status>open</status>
      <hours>
        <hour>
          <from>10am</from>
          <to>6pm</to>
        </hour>
      </hours>
    </times>
    <date>2018-09-29</date>
    <rendered>10am - 6pm</rendered>
  </Saturday>
</week>
</weeks>
</location>

I have been able to get the values of name, date and rendered using the following code

 String URLString = "https://api3.libcal.com/api_hours_grid.php?iid=4246&format=xml&weeks=1&systemTime=0";
        XDocument xdoc = new XDocument();
        xdoc = XDocument.Load(URLString);

        var location = (from p in xdoc.Descendants("location")
                        from s in p.Descendants("week").Elements()
                        //from l in p.Descendants().Where( l => l.Ends)
                        select new
                        {
                            CampusName = (string)p.Element("name"),
                            WeekD = (string)s.Element("date"),
                            OpenHours = (string)s.Element("rendered"),
                            //D = p.Descendants("week").Where(z => p.Element.EndsWith("day"))

                        }).ToList();

I'd like to be able to get the value of those nodes that contain day names, eg Sunday, Monday and so on. I have tried using .EndsWith both in my from statement and in the select new statement, but neither are correct. Can somebody point me in the right direction of how to use it?

With thanks

Sam


Solution

  • If I understand correctly you want to get the day under which that event is stored. To get that you just need to do as below,

    select new
             {
               CampusName = (string)p.Element("name"),
               WeekD = (string)s.Element("date"),
               OpenHours = (string)s.Element("rendered"),
               Day = s.Name.LocalName,
            }).ToList();
    

    This should get you the day for that relative data.