c# - Linq query where clause returning null -


this xml file trying tag element posted_status posted_status has ready

<?xml version="1.0" encoding="utf-8"?> <server>       <network> <---network 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 having problem linq query returning null. trying query xml element. element name posted_status. tag value "ready". trying tag posted_status posted_status equal "ready".

 // query's tag tag equals ready ienumerable<xelement> expiration =             exp in main.elements("posted_status")             (string)exp.element("posted_status").value == "ready"             select exp; 

this execute or calls query action , display values posted_status xml tag tag value equals "ready".

 foreach (string exp in expiration)  {      (int = 0; < intializedpoststat.count(); i++)      {          intializedpoststat[i] = exp.tostring();          lstintializations.items.add("[posted_status]......"               + intializedpoststat[i].tostring());          break;      }  } 

you don't need cast string in where clause need compare against value like

where exp.element("posted_status").value == "ready" 

try:

var expiration =        exp in main.elements("network")        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 parse values propert datetime object)


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -