Question Problem parsing XML, tried everything!

Booster1977

New member
Joined
Jan 23, 2012
Messages
3
Programming Experience
1-3
Hello everyone,

I have a problem regarding parsing XML that I've been fighting with for weeks now and can't seem to resolve. I've read every tutorial and every question on the website here, but either I don't understand it or I want something very special. So you guys are my last hope. I'll state my question as clear as possible.

I request information from a webservice and this webservice gives me an XML reply. I run this XML through a XSLT stylesheet to format it more clearly. After this all I want (and need) to do is get the values out of the XML (String, not file) and store them in a database. The first part is a problem for me, I can't seem to get the individual values out of the string. The XML String looks as follows:

VB.NET:
<openstaandeposten>
  <openstaandepost>
    <Entry>
      <Veldnaam>fin.trs.line.dim2</Veldnaam>
      <Waarde>01249</Waarde>
    </Entry>
    <Entry>
      <Veldnaam>fin.trs.line.openbasevaluesigned</Veldnaam>
      <Waarde>-100.00</Waarde>
    </Entry>
    <Entry>
      <Veldnaam>fin.trs.line.basevaluesigned</Veldnaam>
      <Waarde>-123.00</Waarde>
    </Entry>
    <Entry>
      <Veldnaam>fin.trs.line.invnumber</Veldnaam>
      <Waarde>12345</Waarde>
    </Entry>
    <Entry>
      <Veldnaam>fin.trs.head.code</Veldnaam>
      <Waarde>INK</Waarde>
    </Entry>
    <Entry>
      <Veldnaam>fin.trs.line.matchdate</Veldnaam>
      <Waarde>20111025</Waarde>
    </Entry>
    <Entry>
      <Veldnaam>fin.trs.line.datedue</Veldnaam>
      <Waarde>20100513</Waarde>
    </Entry>
  </openstaandepost>
</openstaandeposten>

I just pasted a part of the XML otherwise my post would become too long, but it's important to know there are multiple <openstaandepost></openstaandepost> that hold the exact same entries, but different data between <Veldnaam> and <Waarde> of course.

I need to get the value of each <Veldnaam> and the <Waarde> in each entry, so I can do a query on it.

I've tried everything and nothing seems to work the way I need it to. Maybe I'm trying stuff that is too difficult. If anyone could help me I would be forever grateful.

Thank you so much in advance!
 
Problem parsing XML, tried everything!
This is quite elementary though.
but it's important to know there are multiple <openstaandepost></openstaandepost>
You haven't put any other significance into that so this example just loops all Entry items, 'xml' is a String variable containing the xml content:
        Dim doc = XDocument.Load(New IO.StringReader(xml))
        For Each entry In doc...<Entry>
            Debug.WriteLine(entry.<Veldnaam>.Value)
            Debug.WriteLine(entry.<Waarde>.Value)
        Next
 
This is quite elementary though.
I know :( I just don't seem to understand it. Thank you for your reply however... I've tried your example and altho it does almost exactly what I need, it only takes out the first 2 lines of each <openstaandepost>. So it only takes out '
fin.trs.line.dim2' and not the entries behind that. Does this mean that I will have to reformat my XML?

You haven't put any other significance into that so this example just loops all Entry items, 'xml' is a String variable containing the xml content:
Ok let me try to explain it more clear.

Each <openstaandepost></openstaandepost> is an invoice. Everything between <openstaandepost> tags are lines from that invoice. So what I want to do is get each name (veldnaam) and its corresponding value (waarde) and put it in a database. Your code appears to be what I'm looking for but it skips all but the first <Entry> of <Openstaandepost>

I'm starting to think it might be a good idea to lose the <Entry> tag altogether?

Once again thank you for helping me.
 
All Entry elements in document is processed with the code I posted, no matter where they occur.
 
OK.. Do this instead and see how you go:

Take that XML and write it to a file, c:\temp\file.xml
Make a new solution, for a test app, WindowsApplication1 or whatever
Put this code in the Form1.Load event:

VB.NET:
Dim xml as String = File.ReadAllText("c:\temp\file.xml") 'this is the equivalent of your ws call, you now have xml in a variable

'forget the XSLT, the parser doesnt care that your XML is ugly as sin
Dim ds as New DataSet
ds.ReadXml(New System.IO.StringReader(xml))

put a breakpoint on the last line (the readXML() line)

Run the program
It hits the breakpoint
STEP OVER one step, it has now read your XML string

Point to the ds variable so the debugger tooltip appears. Click the magnifying glass in the tooltip

Youre looking at the dataset visualizer. This dataset has 2 tables, one for order, and one for order items

Change the displayed table using the combo box. Review the columns.. There is Veldnaam and Waarde columns and openstandepost_ID
All items from one invoice have same openstaandepost_id


You should now be able to work with a dataadapter or at the very least an insert db command and a loop, to upload this data to a db
 
Back
Top