Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML parsing via recursionj

Status
Not open for further replies.

DarwinIT

Programmer
Apr 25, 2008
142
US
I have to parse an XML file coming from a client. I had not worked with XML for a couple years and, as I expected, I ran into some difficulties. I searched high and low on the internet and found a plethora of articles. Some proved to be obsolete. Some just didn't work (dataset). Some I didn't understand exactly how to do them (serialization for one).

I finally thought I had nailed this puppy down. I came up with a recursion routine to convert xml to an arraylist, datatable, or piped string depending on what the developer wants to work with (except XML!!!) Everything seemed to be working great until I discovered that I was returning partial nodes. Turns out the turkeys who originated this data reused tag names - so the principal product which I do a search on to build a nodeList is duplicated.
This is the statement I use to make the list.

XmlNodeList xmlList = xDoc.SelectNodes("//" + elementName); with product being the value passed in to elementName.

Is there anyway I can pass in a duplicated element name so only the node closer to the root is the only one that gets processed? I tried mainelement//product but no go. Still got the child nodes I don't want.

example of xml

<mainelement>

<header>

<merchantId>22</merchantId>
<merchantName>Bogus Merchant </merchantName>
<createdOn>2010-06-29/10:19:48</createdOn>
</header>

<product att1="junk" att2="morejunk" att3="lotsjunk">

<URL>

<product>
</product>

<productImage>
</productImage>

<buy/>

</URL>

<anotherelement> whatever
</anotherelement>

</product>
</mainelement>
 
mainelement/product should work. // is a overload for "any node". if you only want a single node you can also use the SelectNode(query) to select a single node rather than a collection of nodes. you may also want to look into Linq2Xml.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Ahhhh. That did the trick!!! Thank you so much. I was afraid I'd have to tweak the xml tags before processing. Code is up and running! Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top