c# - using xmldocument to read xml -


 <?xml version="1.0" encoding="utf-8" ?>     <testcase>       <date>4/12/13</date>       <name>mrinal</name>       <subject>xmltest</subject>   </testcase> 

i trying read above xml using c#, null exception in try catch block can body suggest required change.

static void main(string[] args)         {                      xmldocument xd = new xmldocument();             xd.load("c:/users/mkumar/documents/testcase.xml");              xmlnodelist nodelist = xd.selectnodes("/testcase"); // <testcase> nodes              foreach (xmlnode node in nodelist) // each <testcase> node             {                 commonlib.testcase tc = new commonlib.testcase();                  try                 {                     tc.name = node.attributes.getnameditem("date").value;                     tc.date = node.attributes.getnameditem("name").value;                     tc.sub = node.attributes.getnameditem("subject").value;                   }                 catch (exception e)                 {                     messagebox.show("error in reading xml", "xmlerror", messageboxbuttons.ok);                 } 

........ .....

the testcase element has no attributes. should looking it's child nodes:

tc.name = node.selectsinglenode("name").innertext; tc.date = node.selectsinglenode("date").innertext; tc.sub = node.selectsinglenode("subject").innertext; 

you might process nodes this:

var testcases = nodelist     .cast<xmlnode>()     .select(x => new commonlib.testcase()     {         name = x.selectsinglenode("name").innertext,         date = x.selectsinglenode("date").innertext,         sub = x.selectsinglenode("subject").innertext     })     .tolist(); 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -