c# - Reading XML file with XmlDocument Nodelist -
is there way utilize selectsinglenode
without entering 'math', 'physics' or 'technologija' read entry?
so far reading goes this:
xmlnodelist nodelist = xd.selectnodes("/studentai/evening"); foreach (xmlnode node in nodelist) { pupil tc = new student(); tc.id = node.attributes.getnameditem("id").value; tc.name = node.attributes.getnameditem("name").value; xmlnode n = node.selectsinglenode("grades"); tc.grade1 = n.childnodes.item(0).innertext; ...
also, since there 'evening' , 'day' students, should utilize nodelist/foreach (xmlnodelist nodelist = xd.selectnodes("/students/evening");
) read them, or can somehow combine /students/evening
, students/day
?
xml file:
<?xml version="1.0" encoding="utf-8" ?> <students> <evening id="36453" name="petras"> <grades> <math> <grade1>5</grade1> <grade2>7</grade2> </math> <technologija> <grade1>8</grade1> <grade2>4</grade2> </technologija> </grades> <average>6.00</average> </evening> <day id="75643" name="jonas"> <grades> <math> <grade1>8</grade1> <grade2>7</grade2> </math> <physics> <grade1>7</grade1> <grade2>10</grade2> </physics> </grades> <average>8</average> </day> <day id="48843" name="andrius"> <grades> <math> <grade1>5</grade1> <grade2>5</grade2> </math> <physics> <grade1>5</grade1> <grade2>7</grade2> </physics> </grades> <average>5.50</average> </day> <evening id="56442" name="antanas"> <grades> <math> <grade1>8</grade1> <grade2>8</grade2> </math> <technologija> <grade1>8</grade1> <grade2>10</grade2> </technologija> </grades> <average>8.50</average> </evening> </students>
yes, can xpath.
really, should never have counter digits in variables or xml elements, work you've got, (or similar) work 2 grades / subject.
// first loop through each pupil foreach( xmlelement ndstudent in xd.selectnodes( "/students/evening | /students/day" ) { // build object info pupil tc = new student(); tc.id = ndstudent.getattribute( "id" ); tc.name = ndstudent.getattribute( "id" ); // add together in grades foreach( xmlelement ndgrade in ndstudent( "grades/*/grade1" ) ) { tc.grade1 = ndgrade.innertext; } // add together in grades foreach( xmlelement ndgrade in ndstudent( "grades/*/grade2" ) ) { tc.grade2 = ndgrade.innertext; } } // end of pupil loop
c# xml object xmldocument nodelist
No comments:
Post a Comment