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>
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>