Issue to read some part of XML file.

Here is my XML file:

<Report>
    <orders>
        <order savedby="manager" time="12:33:09">
            <header saledate="04/Oct/2013" saletime="12:33:09" reqtime="12:33:09" />
            <customer name="-" accname="123465" />
            <payments>
                <payment transid="1" method="7" vposuser="manager" cardtype="" value="2.95"></payment>
            </payments>
            <totals deliverycharge="" splitdelchgstore="" splitdelchgdriver="" sercharge="0.00" />
            <products>
                <product type="cookbook" qty="1" prodid="10000564" listprice="0.00" sellprice="10.00">
                    <stock />
                    <vat />
                    <discount>
                        <discountitem discvalue="10.00" name="Quick On Screen Discount" />
                    </discount>
                </product>
                <product type="" qty="1" description="Crispy Fries" prodid="10000093" listprice="0.00" sellprice="2.95">
                    <stock />
                    <vat />
                    <discount />
                </product>
            </products>
        </order>
        <orders>
            <order savedby="manager" time="12:33:09">
            <header saledate="04/Oct/2013" saletime="12:33:09" reqtime="12:33:09" />
            <customer name="-" accname="123465" />
            <payments>
                <payment transid="1" method="7" vposuser="manager" cardtype="" value="2.95"></payment>
            </payments>
            <totals deliverycharge="" splitdelchgstore="" splitdelchgdriver="" sercharge="0.00" />
            <products>
                <product type="cookbook" qty="1" prodid="10000564" listprice="0.00" sellprice="10.00">
                    <stock />
                    <vat />
                    <discount>
                        <discountitem discvalue="10.00" name="Quick On Screen Discount" />
                    </discount>
                </product>
                <product type="" qty="1" description="Crispy Fries" prodid="10000093" listprice="0.00" sellprice="2.95">
                    <stock />
                    <vat />
                    <discount />
                </product>
            </products>
        </order>
    </orders>
</Report>

I need to get access to:
1) <header saledate="04/Oct/2013" saletime="12:33:09" reqtime="12:33:09" />
2) <customer name="-" accname="123465" />
3) <payment transid="1" method="7" vposuser="manager" cardtype="" value="2.95">

However code that I have cannot read the PAYMENT value, it's empty instead. Here is the code:
Can you help me? This is first time that I am reading XML this way.

Dim settings1 As New XmlReaderSettings()
settings1.ConformanceLevel = ConformanceLevel.Fragment
settings1.IgnoreWhitespace = True
settings1.IgnoreComments = True
Dim reader1 As XmlReader = XmlReader.Create(filePath, settings1)
Dim tempOrderNumber As String = Nothing

While reader1.Read()
    Select Case reader1.NodeType
        Case XmlNodeType.Element
        If reader1.Name = "order" Then
            Dim reader2 As XmlReader = reader1
            While reader1.NodeType <> XmlNodeType.EndElement
                reader1.Read()
                If reader1.Name = "header" Then
                    salenum = reader1.GetAttribute("ordnum")
                    temp_salenum = salenum
                    attendant = reader1.GetAttribute("vposuser")
                    saledate = reader1.GetAttribute("reqdate")
                    saletime = reader1.GetAttribute("reqtime")
                End If

                reader1.Read()
                If reader1.Name = "payment" Then
                    Try
                        saletotalnat = reader1.GetAttribute("value")
                        salesubtotalnat = reader1.GetAttribute("value")
                        paidvalue = FormatNumber(reader1.GetAttribute("value") / VAT, 2)
                    Catch ex As Exception
                    End Try
                End If

                If reader2.NodeType = XmlNodeType.EndElement Then
                    MsgBox("saledate: " & startdate _
                    & vbCrLf & "saletime: " & starttime _
                    & vbCrLf & "sellprice: " & sellprice)
                End If
            End While
        End If
    End Select
End While

Hi jakub.peciak,

the most easy way to read the XML by using XPath in XMLDocument u may use the conditional XPath to select the nodes directly.

Like:
dim salesDate as new dateTime();
dim strAccname as string;
dim XmlDoc as new XmlDcument();
XmlDoc.load(@"D:/XmlFile.xml");
var Transid=XmlDoc.DocumentElement.SelectSingleNode("//header[@saledate='" + salesDate.ToString("dd/MMM/yyyy") + "'][customer/@accname='" + strAccname + "']/payments/payment/@transid").innerText;

like this you can traverse any node in the whole XML at any level with any condition and if you want to check whether the node is there or not then you can check the null of selected node before use the ".InnerText" Property..

if u need anything more let me know please..

Regards..

Ravikash

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.