Hi,

I have a XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
     <Report>
          <config repstarttime="" rependtime=""></config>
          <cashdrops></cashdrops>
          <cashsheets></cashsheets>
          <cspdata></cspdata>
          <fdremdata>
               <fdrementry  ENTRYID=""></fdrementry>
          </fdremdata>
               <SHIFTDATA>
                    <SHIFT_CD_Index ShiftSystemIndex="1" Shift_CD_Used="1" ></SHIFT_CD_Index>
                    <SHIFT_CD_Index ShiftSystemIndex="2" Shift_CD_Used="1" ></SHIFT_CD_Index>
                    <SHIFT_CD_Index ShiftSystemIndex="3" Shift_CD_Used="1" ></SHIFT_CD_Index>
                    <SHIFT_CD_Index ShiftSystemIndex="4" Shift_CD_Used="1" ></SHIFT_CD_Index>
               </SHIFTDATA>
     <Order>
               <OrderHead  filelocation="" filesuccess="TRUE" Attendant=""></OrderHead>
               <NFC1DATA nfc1count="0"></NFC1DATA>
               <PAYMENTDATA paymentcount="1">
                    <payentrydata id="1"></payentrydata>
               </PAYMENTDATA>
               <CASHBACKDATA cashbackcount="0"></CASHBACKDATA>
                    <Product>
                         <productdata ProdCompActive="FALSE" ProdDesc="Product1" ProdIDNum="00001" ProdQty="1">
                              <productsession>
                                   <productsessiondata>[CONTENT1]   *1;</productsessiondata>
                                   <productsessiondata>[CONTENT2]   *1;</productsessiondata>
                                   <productsessiondata>[CONTENT3]   *1;</productsessiondata>
                                   <productsessiondata>[CONTENT4]   *1;</productsessiondata>
                                   <productsessiondata>[CONTENT5]   *4;</productsessiondata>
                                   <productsessiondata>[CONTENT6]   *4;</productsessiondata>
                              </productsession>
                         </productdata>
                    </Product>
                    <histdata id="1" SYS_ID="1" DTE="20120401"></histdata>
                    <histdata id="2" SYS_ID="1" DTE="20120401"></histdata>
                    <histdata id="3" SYS_ID="0" DTE="20120401"></histdata>
                    <histdata id="4" SYS_ID="1" DTE="20120401"></histdata>
                    <histdata id="5" SYS_ID="0" DTE="20120401"></histdata>
                    <FooterDATA footercount="0"></FooterDATA>
     </Order>
     </Report>

and I need to read PRODUCT informations.
The script that I made reads details from "productdata" and only first line from "productsessiondata". Could you advise how I can read all "productsessiondata" details, please?

Here is the code in vb 2008:

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim file As String = "c:\db\test.xml"
    Dim doc As XDocument = XDocument.Load(file)
    For Each product As XElement In doc.Descendants("productdata")
        MessageBox.Show("ID : " + product.Attribute("ProdDesc").Value)
    Next
    For Each product As XElement In doc.Descendants("productsession")
        If product.HasElements Then
            MessageBox.Show("ID : " + product.Element("productsessiondata").Value)
        End If
    Next
    End Sub
End Class

You just need another For Each loop, instead of the If statement, to iterate through each XElement of product.Elements("productsessiondata").

I have change the code and tried many times but I sill can read only a first line of the "productsessiondata"

Here is the lates code:

Dim file As String = "c:\db\test.xml"
Dim doc As XDocument = XDocument.Load(file)
   For Each product As XElement In doc.Descendants("productdata")
        MessageBox.Show("ProdDesc : " + product.Attribute("ProdDesc").Value)

         For Each product1 As XElement In doc.Descendants("productsession") 
                For Each product2 As XElement In doc.Descendants("productsession")
                    MessageBox.Show("Item : " + product2.Element("productsessiondata").Value)
                Next
         Next
    Next

Any advises?

You're inner for-loop should be referencing elements from product, not descendants of the document, and the inner-most for-loop should be referencing elements from product1...

Here... to get you started:

For Each product1 As XElement In product.Elements("productsession")
    ...
Next

Hey, nearly there. It is working but the value from Element "productsessiondata" is always the same (reading only the first line)

code:

Dim file As String = "c:\db\test.xml"
Dim doc As XDocument = XDocument.Load(file)
For Each product As XElement In doc.Descendants("productdata")
   MessageBox.Show("ProdDesc : " & product.Attribute("ProdDesc").Value)
   If product.HasElements Then
      For Each product1 As XElement In product.Elements("productsession")
          If product1.HasElements Then
              For Each product2 As XElement In product1.Elements("productsessiondata")
                  MessageBox.Show("Item : " & product1.Element("productsessiondata").Value)
              Next
          End If
      Next
   End If
Next

when I change the line
"MessageBox.Show("Item : " & product1.Element("productsessiondata").Value)"
to "MessageBox.Show("Item : " & product2.Element("productsessiondata").Value)"
I am getting an error "Object reference not set to an instance of an object."

product1.Element("productsessiondata") returns the first child element, with the name "productsessiondata", of product1. In the for-loop, you're iterating through each of the child elements, which is stored in product2. So there's no need to get the child element from product2, you want its value directly (i.e. product2.Value).

Working! Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.