0

This is the XML File I am trying to get the tag Element Posted_Status where Posted_Status has Ready

<?xml version="1.0" encoding="utf-8"?>
<Server>
      <Network> <---Network is the parent element
        <Posted_Status id="10">Ready</Posted_Status>
        <Timed_On id="10">7/28/2013 9:32:10 AM</Timed_On>
        <Timed_Off id="10">8/28/2013 9:32:10 AM</Timed_Off>
      </Network>
</Server>

I am having a problem where a linq query is returning null. I am trying to query an XML element. The element name is Posted_Status. The tag value is "Ready". I am trying to get the tag Posted_Status where the Posted_Status is equal to "Ready".

 // Query's the tag where the tag equals Ready
IEnumerable<XElement> expiration =
            from exp in main.Elements("Posted_Status")
            where (string)exp.Element("Posted_Status").Value == "Ready"
            select exp;

This execute or calls the query into action and display all the values from the Posted_Status XML tag where the tag value equals "Ready".

 foreach (string exp in expiration)
 {
     for (int i = 0; i < IntializedPostStat.Count(); i++)
     {
         IntializedPostStat[i] = exp.ToString();
         lstIntializations.Items.Add("[Posted_Status]......" 
             + IntializedPostStat[i].ToString());
         break;
     }
 }
shawn
  • 113
  • 1
  • 1
  • 15

2 Answers2

0

You don't need to cast to string in where clause also you need to compare it against Value like

where exp.Element("Posted_Status").Value == "Ready"

Try:

var expiration =
       from exp in main.Elements("Network")
       where exp.Element("Posted_Status").Value.Equals("Ready", StringComparison.CurrentCulture)
       select
       new
       {
           Timed_On = exp.Element("Timed_On").Value,
           Timed_Off = exp.Element("Timed_Off").Value,
       };

For Ouput:

foreach (var item in expiration)
{
    Console.WriteLine("Timed_On: {0} \r\nTimed_Off: {1}", item.Timed_On, item.Timed_Off );
}

(Its better if you parse the values to a propert DateTime object)

Habib
  • 219,104
  • 29
  • 407
  • 436
  • for some reason it comes back null still could it be the way I am calling or executing the query or something – shawn Jul 31 '13 at 17:46
  • @Habib isn't he missing ToList() to get results? – Ehsan Jul 31 '13 at 17:51
  • 1
    @EhsanUllah, not really, while iterating using `foreach` loop, it would execute (iterate) the query. – Habib Jul 31 '13 at 17:53
  • `XElement` defines cast operators to return `Value` in an appropriate format; there's no real difference between what you and the OP are doing except for the case insensitivity. – anton.burger Jul 31 '13 at 17:56
  • @Habib now that is new for me. – Ehsan Jul 31 '13 at 17:56
  • should it have a IEnumerable expiration = from exp in main.Elements("Posted_Status") where exp.Element("Posted_Status").Value.Equals("Ready", StringComparison.InvariantCultureIgnoreCase) select exp; – shawn Jul 31 '13 at 17:57
  • @EhsanUllah, when ever a call to `ToArray` or `ToList` is made they query gets iterated, Its same with `foreach` loop. – Habib Jul 31 '13 at 17:58
  • @shawn, post your sample XML, because I think shambulator is right, post the XML in your question. – Habib Jul 31 '13 at 17:58
  • I just posted the xml – shawn Jul 31 '13 at 18:04
  • @shawn, what exactly do you need from `Posted_status` do you need the ID attribute value ? – Habib Jul 31 '13 at 18:09
  • no I need the value from posted_status some xml files may have posted_status that has a value different from ready value I just want the values from posted status that are with ready value and get those values – shawn Jul 31 '13 at 18:15
  • @shawn, value from Posted_status is `Ready`, if you want next nodes then they are not related to Posted_status, they are under `Network` element. – Habib Jul 31 '13 at 18:17
  • I want the Posted_Status Value Where the Posted_Status Value is Ready – shawn Jul 31 '13 at 18:21
  • I am trying to do it with out the attribute value I need to know the data in the xml tag to verify if I need it or not think like filtering I need to filter out other values except the Ready value – shawn Jul 31 '13 at 18:24
0

Both your from and where read Element("Posted_Status").

Edit based on updated question, this should be it:

XElement main = XDocument.Load(fi.FullName).Element("Server");
var expiration = from exp in main.Elements("Network")
 where exp.Element("Posted_Status").Value == "Ready"
 select exp;

You have to read the root element first. Then you iterate through all "Network" and check the "Posted_Status" value

This will return all "Network" elements that suit the condition

Ondra
  • 1,619
  • 13
  • 27