XmlReader.ReadtoFollowing has state EndofFile why? -


i've produced code read xml file string, has problems. notably readtofollowing() method returns nothing. seems seek whole xmlstring, set xmlreader state endoffile. i'm puzzled this, readstartelement() works , next element read "heading" you'd expect.

here's code, idea read through xml pulling out fields require;

list<string> contentfields = new list<string>() { "heading", "shortblurb", "description" };  string xml = @"<filemeta filetype='audio'><heading>fatigue &amp; tiredness</heading><shortblurb>shortblurb</shortblurb><description /><comments /><albumtitle /><tracknumber /><artistname /><year /><genre /><tracktitle /></filemeta>";  using (xmlreader reader = xmlreader.create(new stringreader(xml))) {     reader.readstartelement("filemeta");      foreach (string field_str in contentfields)     {         reader.readtofollowing(field_str);          if (reader.name.tostring() == field_str)         {             console.writeline(field_str + " " + reader.readelementcontentasstring());         }     } }  console.readkey(); 

that's because reader.readstartelement("filemeta"); position reader on xml tag heading. readtofollowing 1 read (reading past heading tag) , start seek element name heading. read past it, readtofollowing not find anymore , read end of file.

if want avoid this, change code :

list<string> contentfields = new list<string>() { "heading", "shortblurb", "description" };  string xml = @"<filemeta filetype='audio'><heading>fatigue &amp; tiredness</heading><shortblurb>shortblurb</shortblurb><description /><comments /><albumtitle /><tracknumber /><artistname /><year /><genre /><tracktitle /></filemeta>";  using (xmlreader reader = xmlreader.create(new stringreader(xml))) {     reader.readstartelement("filemeta");      foreach (string field_str in contentfields)     {         if (reader.name.tostring() != field_str)         {             reader.readtofollowing(field_str);         }          //still keep if because have reached end of xml document         if (reader.name == field_str)         {             console.writeline(field_str + " " + reader.readelementcontentasstring());         }     } }  console.readkey(); 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -