XML : C# xmlReader: searching for the value of an element [duplicate]

This question already has an answer here:

There is probably a simple solution for this but I haven't worked with XML before and I'm new to C# (moving from Java). I need to parse this XML from Wolfram Alpha (shortened because I'm only interested in the first half of the data):

   <?xml version='1.0' encoding='UTF-8'?>  <queryresult success='true'      error='false'      numpods='4'      datatypes='ExpandedSalary'      timedout='Data,Money,UnitInformation,Error'      timedoutpods=''      timing='1.927'      parsetiming='0.383'      parsetimedout='false'      recalculate='http://www5a.wolframalpha.com/api/v2/recalc.jsp?id=MSPa16751fibf36dddi3723h0000268a1b90f16ad7i9&amp;s=24'      id='MSPa16761fibf36dddi3723h00004h3ib55b67h4c124'      host='http://www5a.wolframalpha.com'      server='24'      related='http://www5a.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa16771fibf36dddi3723h0000403753h31d9afbe0&amp;s=24'      version='2.6'>   <pod title='Input interpretation'       scanner='Identity'       id='Input'       position='100'       error='false'       numsubpods='1'>    <subpod title=''>     <plaintext>computer software applications engineers | median wage | annual wage  United States</plaintext>     <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP16781fibf36dddi3723h00005888h2bc419788b9?MSPStoreType=image/gif&amp;s=24'         alt='computer software applications engineers | median wage | annual wage  United States'         title='computer software applications engineers | median wage | annual wage  United States'         width='496'         height='61' />    </subpod>    <definitions count='1'>     <definition word='computer software applications engineers'         desc='Develop, create, and modify general computer applications software or specialized utility programs. Analyze user needs and develop software solutions. Design software or customize software for client use with the aim of optimizing operational efficiency. May analyze and design databases within an application area, working individually or coordinating database development as part of a team. Exclude &quot;Computer Hardware Engineers&quot; (17-2061).' />    </definitions>   </pod>  <pod title='Result'       scanner='Data'       id='Result'       position='200'       error='false'       numsubpods='1'       primary='true'>    <subpod title=''>     <plaintext>$87480 per year  (US dollars per year)  (2009)</plaintext>     <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP16791fibf36dddi3723h000054648g42b9ce64c4?MSPStoreType=image/gif&amp;s=24'         alt='$87480 per year  (US dollars per year)  (2009)'         title='$87480 per year  (US dollars per year)  (2009)'         width='289'         height='18' />    </subpod>   </pod>    

I'm looking for the value of <plaintext>$87480 per year (US dollars per year) (2009)</plaintext>

I have tried couple of ways of doing this but here is my current code (which, of course, doesn't work. Specifically, it gives me an empty string):

  using (XmlReader reader = XmlReader.Create(new StringReader(val)))  {      while (reader.ReadToFollowing("pod")) //start with the first pod      {           reader.ReadToDescendant("subpod"); //go to the subpod          reader.ReadToDescendant("plaintext");//go           wage = reader.Value;          output.AppendLine(wage);          if (!wage.Equals(" "))  //trying to see where the empty string is coming from       return wage;       }    }  return wage;    }    

I have also tried Linq with no luck. Any help is appreciated.

No comments:

Post a Comment