c# - How to access a certain element and retrieve the data XML -


my xml looks this. can contain n numbers of modules , n number of assignments. assignments nested in modules contains:

<?xml version="1.0"?> <course name="engineering"> <level4>     <module name="electric" creditval="22">         <assignment name="wer" score="22" weight="50">         </assignment>         <assignment name="asd" score="50" weight="50">         </assignment>     </module>     <module name="materials" creditval="22">         <assignment name="ghj" score="22" weight="75>         </assignment>         <assignment name="kl" score="80" weight="15">         </assignment>     </module> </level4> </course> 

i accessing modules , assignments following:

xpathdocument xpd = new xpathdocument("xmlfile.xml");  //getting modules in level 4 foreach (xpathnavigator mod in xpd.createnavigator().select("/course/level4/module")) {     //accessing module elemtns     if (mod.hasattributes)     {         module modtoadd = new module();         modtoadd.name = mod.getattribute("name", "");         console.writeline("module name: " + modtoadd.name);         modtoadd.creditvalue = int.parse(mod.getattribute("creditval", ""));         console.writeline("module cred: " + modtoadd.creditvalue);         modtoadd.assignments = new list<assignment>();         //accessing assignment elements within module element         foreach (xpathnavigator asgn in xpd.createnavigator().select("course/level4/module/assignment"))         {             assignment asn = new assignment();             asn.name = asgn.getattribute("name", "");             console.writeline(asn.name);             asn.weighting = int.parse(asgn.getattribute("weight", ""));             console.writeline(asn.weighting);             asn.usersscore = int.parse(asgn.getattribute("score", ""));             console.writeline(asn.usersscore);             modtoadd.assignments.add(asn);         };         coursexml.level_41.add(modtoadd);     } }; 

what supposed happen: xmlreader reads assignment nested in relative modules resides in. e.g. if total module score 'eletric' should worth 72 , 'materials' should worth 102.

what happens: after testing realised there wrong logic. works fine when 1 module saved if there more 1 assignments added every module. if total module score materials , electric both worth 174.

i new xml , reading/saveing files. :)

i find linq2xml easier use.

var xdoc = xdocument.load(filename); var res  = xdoc.descendants("module")             .select(m => new             {                 name = (string)m.attribute("name"),                 creditval = (int)m.attribute("creditval"),                  assignments = m.descendants("assignment")                                 .select(a => new                                 {                                     name = (string)a.attribute("name"),                                     score = (int)a.attribute("score"),                                     weight = (int)a.attribute("weight")                                 })                                 .tolist()             })             .tolist(); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -