c# - Reading several elements using Linq-to-Xml and some loop? -


with following code, i'm reading <ns>countersales</ns> (the 3rd element) of every <nv>, great. i'm including code , xml.

here's code, works great if elementat hardcoded :

var xdoc = xdocument.parse(xmlstr); var nvs = xdoc.descendants("nv"); var nads = nvs.select(nv => nv.elements("nad").first().value).tolist(); var thirdrs = nvs.select(nv => nv.elements("r").elementat(2).value).tolist(); 

here's value of xmlstr:

<ni>     <nss>20150927</nss>     <gp>addon</gp>     <ns>counterblah1</ns>     <ns>counterblah2</ns>     <ns>countersales</ns>     <ns>counterblah4</ns>     <ns>counterblah5</ns>     <ns>counterblah6</ns>     <nv>         <nad>style=1,rfu=1,id=132</nad>         <r>0</r>         <r>15</r>         <r>8</r>         <r>3</r>         <r>2</r>         <r>2</r>     </nv>     <nv>         <nad>style=1,rfu=1,id=433</nad>         <r>0</r>         <r>15</r>         <r>30</r>         <r>3</r>         <r>2</r>         <r>2</r>     </nv>     <nv>         <nad>style=1,rfu=1,id=665</nad>         <r>0</r>         <r>15</r>         <r>90</r>         <r>3</r>         <r>2</r>         <r>2</r>     </nv> </ni> 

but let's read more 1 counter. have datatable has 2 rows: [countersales] & [counterblah5]. process other 2 counters same way:

int index = 0; datatable results = getcounters(); //results has 2 rows: [countersales] & [counterblah5]  foreach (datarow row in results.rows) {     string counter = cellrow["counter"]; //counter equals "countersales"     /* iterate through <ns> see "countersales" located.        in case, index = 2. on next foreach, index equals 4  */      var xdoc = xdocument.parse(xmlstr);     var nvs = xdoc.descendants("nv");     var nads = nvs.select(nv => nv.elements("nad").first().value).tolist();     var thirdrs = nvs.select(nv => nv.elements("r").elementat(index).value).tolist(); } 

any appreciated.

thanks.

if understood problem correctly, need

datatable results = getcounters(); //results has 2 rows: [countersales] & [counterblah5]  foreach (datarow row in results.rows) {     string counter = cellrow["counter"]; //counter equals "countersales"     /* iterate through <ns> see "countersales" located.         in case, index = 2. on next foreach, index equals 4  */                      var xdoc = xdocument.parse(xmlstr);      //here find index     var requiredindex = xdoc.descendants("ns")             .select((node, idx) => new { node, idx})             .first(x => x.node.value.equals(counter)).idx;      var nvs = xdoc.descendants("nv");     var nads = nvs.select(nv => nv.elements("nad").first().value).tolist();     var thirdrs = nvs.select(nv => nv.elements("r").elementat(requiredindex).value).tolist(); } 

edit: shorter syntax find index list<t> this post.

var requiredindex = xdoc.descendants("ns").tolist().findindex(x => x.value == counter); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -