c# - How to find specific data in XML POST Response? -
i'm tasked retrieving specific data xml post response.
here's example of xml reply:
<servicehistoryarray> <recordid>xd116067*401529</recordid> <swr> <v idx="1">7932</v><v idx="2">7932</v><v idx="3">7932</v><v idx="4">7932</v> </swr> <comments/> <mileage>6</mileage> <operation> <v idx="1">z7000*wpdip****pre-delivery inspection - base time</v><v idx="2">z911*inc****etch windows</v><v idx="3">b0048*wpbs4****fascia, front bumper - replace 1 piece</v><v idx="4">9997*wpbs4****</v> </operation> <closedate>1998-12-30</closedate> <opendate>1998-10-16</opendate> <partsamount> <v idx="1">0.00</v><v idx="2">0.00</v><v idx="3">166.32</v><v idx="4">0.00</v> </partsamount> <laboramount> <v idx="1">64.80</v><v idx="2">3.60</v><v idx="3">140.40</v> </laboramount> <warranty> <s idx="1">ü96ü0ýý10232632ü4hü1</s><s idx="2">ü96ü0ýý10232632ü4hü1</s><s idx="3">ü96ü0ýý10232632ü4hü1</s> </warranty> </servicehistoryarray>
i need concatenate of related "v" elements' inner text.
ex:
7392 z700*wdip****pre-delivery inspection - base time 0.00 64.80
7392 z911*inc****etch windows 0.00 3.60
etc...
here's code far:
// string variable store xml post reply string replystring = webrequestpostdata("http://" + maskedtextbox1.text + ":" + maskedtextbox2.text, payload); xmldocument replydoc = new xmldocument(); // new xmldocument reply replydoc.loadxml(replystring); xmlnodelist list = replydoc.selectnodes("//servicehistoryarray/*"); // xmlnodelist based on child elements of <servicehistoryarray> node. var nodees = new list<xmlnode>(list.cast<xmlnode>().toarray()); // finds mileage, closedate, , opendate values , displays them on form. foreach (xmlnode node in nodees) { switch (node.name) { case "opendate": textbox1.text = node.innertext; break; case "closedate": textbox2.text = node.innertext; break; case "mileage": textbox3.text = node.innertext; break; } } // section supposed display concatenated values of <v> elements: (int = 0; < nodees.count; i++) { if (nodees[i].haschildnodes) { (int j = 0; j < nodees[i].childnodes.count; j++) { switch (nodees[i].childnodes[j].nodetype) { case xmlnodetype.text: break; case xmlnodetype.element: switch (nodees[i].childnodes[j].attributes[0].value) { case "1": console.writeline("1-" + nodees[i].childnodes[j].innertext + " "); break; case "2": console.writeline("2-" + nodees[i].childnodes[j].innertext + " "); break; case "3": console.writeline("3-" + nodees[i].childnodes[j].innertext + " "); break; case "4": console.writeline("4-" + nodees[i].childnodes[j].innertext + " "); break; } break; } } } }
the concatenated values need displayed in listbox control. additionally, need try , code process not anticipate set number of elements; in case there 4. other instances of post request may return 10 or 2 or whatever.
i realize may or may not impact other users i'm not 1 out there problem...
you can use linq xml:
xdocument xdoc = xdocument.parse(replystring); var query = v in xdoc.descendants("v") group v (int)v.attribute("idx") g select string.join(" ", g.select(x => (string)x));
this query selects v
elements in document , groups them idx
atribute value (in order elements appear in document). values of elements in group concatenated string. query contain:
"7932 z7000*wpdip****pre-delivery inspection - base time 0.00 64.80" "7932 z911*inc****etch windows 0.00 3.60" "7932 b0048*wpbs4****fascia, front bumper - replace 1 piece 166.32 140.40" "7932 9997*wpbs4**** 0.00"
Comments
Post a Comment