xml beginners question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have an xml file that contains the following data at the bottom of this
email..

I want to step through each getting the date and the pick qty. In my code
here I only every get one, the first value back..

Is my vb code wrong, or my xml file not proper ?

m_picklist = m_xmld.SelectNodes("/LogData/Picks")
For Each m_node In m_picklist
Dim pDate = m_node.ChildNodes.Item(0).InnerText
Dim pCount = m_node.ChildNodes.Item(1).InnerText
Next

................
(xml extract)
<LogData>
<Serial>A-01-03-01</Serial>
<SQLVersion>Not Set</SQLVersion>
<ClientVersion>1.0d</ClientVersion>
- <Picks>
<Date>06/11/2009</Date>
<Count>52</Count>
<Date>06/10/2009</Date>
<Count>52</Count>
<Date>06/09/2009</Date>
<Count>76</Count>
<Date>06/06/2009</Date>
<Count>1</Count>
<Date>06/05/2009</Date>
<Count>37</Count>
<Date>06/04/2009</Date>
<Count>69</Count>
<Date>06/03/2009</Date>
<Count>44</Count>
<Date>06/02/2009</Date>
<Count>39</Count>
<Date>06/01/2009</Date>
<Count>47</Count>
</Picks>
.............
 
NoSpammer said:
Hi

I have an xml file that contains the following data at the bottom of this
email..

I want to step through each getting the date and the pick qty. In my code
here I only every get one, the first value back..

Is my vb code wrong, or my xml file not proper ?

m_picklist = m_xmld.SelectNodes("/LogData/Picks")
For Each m_node In m_picklist
Dim pDate = m_node.ChildNodes.Item(0).InnerText
Dim pCount = m_node.ChildNodes.Item(1).InnerText
Next

................
(xml extract)
<LogData>
<Serial>A-01-03-01</Serial>
<SQLVersion>Not Set</SQLVersion>
<ClientVersion>1.0d</ClientVersion>
- <Picks>
<Date>06/11/2009</Date>
<Count>52</Count>
<Date>06/10/2009</Date>
<Count>52</Count>
<Date>06/09/2009</Date>
<Count>76</Count>
<Date>06/06/2009</Date>
<Count>1</Count>
<Date>06/05/2009</Date>
<Count>37</Count>
<Date>06/04/2009</Date>
<Count>69</Count>
<Date>06/03/2009</Date>
<Count>44</Count>
<Date>06/02/2009</Date>
<Count>39</Count>
<Date>06/01/2009</Date>
<Count>47</Count>
</Picks>
.............

.

You only have one "Picks" element, so the loop only happens one time. You
should surround each pair of "Date" and "Count" elements with a "Picks"
element, then I believe your code would be fine.

If you think that your picks element should surround multiple pairs, then
you need to change your loop logic. It looks like a "Pick" has a "Date" and
"Count", so I think the xml needs restructuring.

Mike
 
NoSpammer said:
Is my vb code wrong, or my xml file not proper ?

m_picklist = m_xmld.SelectNodes("/LogData/Picks")
For Each m_node In m_picklist
Dim pDate = m_node.ChildNodes.Item(0).InnerText
Dim pCount = m_node.ChildNodes.Item(1).InnerText
Next

...............
(xml extract)
<LogData>
<Serial>A-01-03-01</Serial>
<SQLVersion>Not Set</SQLVersion>
<ClientVersion>1.0d</ClientVersion>
- <Picks>
<Date>06/11/2009</Date>
<Count>52</Count>
<Date>06/10/2009</Date>
<Count>52</Count>

With that XML you need e.g.
For Each dateEl as XmlElement In
m_xmld.SelectNodes("/LogData/Picks/Date")
Dim dateStr As String = dateEl.InnerText
Dim count As String =
dateEl.SelectSingleNode("following-sibling::Count[1]").InnerText
Next
 
NoSpammer said:
I want to step through each getting the date and the pick qty. In my
code here I only every get one, the first value back.

That would be because you're selecting the Picks element and repeatedly
(if only once) processing that one.

I would have hoped to see something more like :

<LogData>
<Serial>A-01-03-01</Serial>
<SQLVersion>Not Set</SQLVersion>
<ClientVersion>1.0d</ClientVersion>
<Picks>
<Pick Date="06/11/2009">52</Pick>
<Pick Date="06/09/2009>76</Pick>
<Pick Date="06/06/2009>1</Pick>
<Pick Date="06/05/2009>37</Pick>
<Pick Date="06/04/2009>69</Pick>
<Pick Date="06/03/2009>44</Pick>
<Pick Date="06/02/2009>39</Pick>
<Pick Date="06/01/2009>47</Pick>
</Picks>
</LogData>

That way, you can loop through each "Pick" element using

m_picklist = m_xmld.SelectNodes(" / LogData / Picks / Pick ")
For Each m_node As XmlElement In m_picklist
Dim pDate = m_node.GetAttribute("Date")
Dim pCount = m_node.InnerText
Next

(I should probably mention; I like Attributes in my Xml but I know that
lots of people /don't/(!). Apologies if this thread sparks off into
another Holy war).

HTH,
Phill W.
 
Back
Top