Parsing XML file in C# -


i have xml file, generated library written in c++:

<?xml version="1.0" encoding="utf-8" ?> <main>     <days>         <day>             <date>27.06.2014</date>             <p/>         </day>         <day>             <date>28.06.2014</date>             <p>                 <short>short value</short>                 <name>name value</name>                 <type>type value</type>                 <aud>aud value</aud>                 <tr>tr value</tr>                 <added>added value</added>             </p>             <p>                 <short>short value</short>                 <name>name value</name>                 <type>type value</type>                 <aud>aud value</aud>                 <tr>tr value</tr>                 <added>added value</added>             </p>         </day>     ...     </days>     <di>         <did>             <dayname>Пн</dayname>             <sw>1</sw>             <st>8:00-9:35</st>         </did>                       <did>             <dayname>Вт</dayname>             <sw>2</sw>             <st>9:45-11:20</st>         </did>       ...     </di> </main> 

i read these xml file below:

xmltextreader r = new xmltextreader("out.xml"); while (r.read()) {     if (r.name == "date") { r.read(); dates[c1] = (r.value); c1++; }     else if (r.name == "short") { r.read(); shorts[c2] = (r.value); c2++;  }     else if (r.name == "name") { r.read(); names[c3] = (r.value); c3++;  }     else if (r.name == "type") { r.read(); types[c4] = (r.value); c4++; }     else if (r.name == "aud") { r.read(); auds[c5] = (r.value); c5++; }     else if (r.name == "tr") { r.read(); trs[c6] = (r.value); c6++; }     else if (r.name == "sw") { r.read(); ws[c7] = (r.value); c7++; }     else if (r.name == "st") { r.read(); st[c8] = (r.value); c8++; } } r.close(); 

how parse file, can understand parameter day belongs, if there several in 1 day ("p" in case)? trying find solution in internet, many people better use "linq xml" or "xpath", how maintain object dependencies nobody says.

load xml using xdocument class , can traverse using descendants or element methods.

var xml = xdocument.load("your xml string"); foreach (var day in xml.descendants("day")) {     var pchildren = day.descendants("p").tolist();     var arettheremoreps = pchildren.count() > 1;     foreach (var p in pchildren)     {         var shortval = (string)p.element("short");         //etc     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -