xml - how to parse multiple elements for a particular value -


i receiving string of xml web service , parsing pick few values need save. putting string xmldocument , using xmlnodereader parse element name , grab value.

this fine apart when value after named, such type, id or source in snippet:

<webresult>     <header>       <type>success</type>       <id>52347</id>     </header>   <source>global</source>    <returnitems>     <returnitem>       <dataname>saleid</dataname>       <datavalue>co12345</datavalue>     </returnitem>     <returnitem>       <dataname>productid</dataname>       <datavalue>xy000001</datavalue>     </returnitem>   </returnitems> </webresult> 

however, i'm not sure of best way pickup salesid's 'datavalue' value of co12345 in example. (the xml schema larger)

i parse string saleid + x characters break if web service changed , relying on string positions. prefer not have serialize class seems there should simple way this. (which missing!)

this easy linq xml, if you're able (or willing to) use it.

' xdocument.parse load xml string ' use xdocument.load load file dim xdoc xdocument = xdocument.parse(xmlstring)  dim salesid = (from x in xdoc.descendants("returnitem")               x.element("dataname").value = "saleid"               select x.element("datavalue").value).firstordefault()  

what above code snippet first gets "returnitem" elements (and children) in xdoc.

next, filters on "dataname" elements have salesid value.

then selects corresponding "datavalue" element's value.

firstordefault returns first element matches (or default value if nothing found). without firstordefault() collection of matching results iterate through.

the above pretty trivial example, linq xml powerful , prefer when dealing xml.

edited add non-linq approach

here's way above using xmldocument , xpath syntax:

dim xmldoc new xmldocument() ' use loadxml load string; if file use load xmldoc.loadxml(xmlstring)  dim saleid string  dim returnitems xmlnodelist = xmldoc.selectnodes("/webresult/returnitems/returnitem")  each item xmlnode in returnitems     if item.selectsinglenode("dataname").innertext = "saleid"         saleid = item.selectsinglenode("datavalue").innertext     end if next 

the above snippet of code loads xml string xmldocument. then, selects nodes match xpath expression "/webresult/returnitems/returnitem" (i.e., grabs returnitem nodes , children).

next, iterates through collection, , each returnitem node, checks see if dataname node equal "saleid", , if assigns value of datavalue node saleid string.

this is, again, trivial example. if have multiple "saleid" nodes in xml you'll need put values in list or other collection, otherwise above code give last one.

xpath powerful, , rich support xml , xpath has been part of .net framework since 1.0.

the advantage here if format of message ever changes, you'll need update xpath queries, rather figuring out how parse new format.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -