Question : Loop through XML file using VB.Net

Hi Guys

Below is a sample of an XML file

<?xml version="1.0" encoding="UTF-8" ?>
<DsOrders xmlns="http://www.tempuri.org/DsOrderInfo.xsd">
      <Orders>
            <OrderID>WEB201006293</OrderID>
            <Company></Company>
            <DelContactName>maria barrett</DelContactName>
            <DelCompany></DelCompany>
            <DelAddress1>23 buttermere close</DelAddress1>
            <DelAddress2></DelAddress2>
            <DelCounty>kent</DelCounty>
            <DelPostCode>ME7 2TS</DelPostCode>
            <DelTown>gillingham</DelTown>
            <DelCountry>GB</DelCountry>
            <OrderDate>2010-06-29 09:25:40</OrderDate>
            <Carriage>1.2766</Carriage>
            <CarriageTaxCode>1</CarriageTaxCode>
            <tax>1.93</tax>
            <DelEmail>barrettmaria@hotmail.com</DelEmail>
            <DelTel>07800937785</DelTel>
            <DelFax></DelFax>
            <AddressType>R</AddressType>
            <CustomerType>Personal use</CustomerType>
            <TutorNo></TutorNo>
            <PaymentRef>WEB201006293</PaymentRef>
            <Payment>14.49</Payment>
            <PaymentType>SR</PaymentType>
            <BankPostingCode>1201</BankPostingCode>
            <CarriageNominalCode>4401</CarriageNominalCode>
      </Orders>
      <Orders>
            <OrderID>WEB201006295</OrderID>
            <Company></Company>
            <DelContactName>Neil Hart</DelContactName>
            <DelCompany></DelCompany>
            <DelAddress1>Flat 2 Lansdowne Court</DelAddress1>
            <DelAddress2>25 Lansdowne Road</DelAddress2>
            <DelCounty>East Sussex</DelCounty>
            <DelPostCode>BN3 1GB</DelPostCode>
            <DelTown>Hove</DelTown>
            <DelCountry>GB</DelCountry>
            <OrderDate>2010-06-29 10:30:29</OrderDate>
            <Carriage>1.50</Carriage>
            <CarriageTaxCode>0</CarriageTaxCode>
            <tax>0.00</tax>
            <DelEmail>[email protected]</DelEmail>
            <DelTel>07989107285</DelTel>
            <DelFax></DelFax>
            <AddressType>R</AddressType>
            <CustomerType></CustomerType>
            <TutorNo></TutorNo>
            <PaymentRef>WEB201006295</PaymentRef>
            <Payment>8.00</Payment>
            <PaymentType>SR</PaymentType>
            <BankPostingCode>1201</BankPostingCode>
            <CarriageNominalCode>4401</CarriageNominalCode>
      </Orders>
      <Orders>
            <OrderID>WEB201006296</OrderID>
            <Company></Company>
            <DelContactName>Lydia Jarvis</DelContactName>
            <DelCompany></DelCompany>
            <DelAddress1>2 Edward Street</DelAddress1>
            <DelAddress2>Trecynon</DelAddress2>
            <DelCounty>R.C.T</DelCounty>
            <DelPostCode>CF44 8LW</DelPostCode>
            <DelTown>Aberdare</DelTown>
            <DelCountry>GB</DelCountry>
            <OrderDate>2010-06-29 10:45:54</OrderDate>
            <Carriage>1.50</Carriage>
            <CarriageTaxCode>0</CarriageTaxCode>
            <tax>0.00</tax>
            <DelEmail>lyddiej@hotmail.com</DelEmail>
            <DelTel>01685874500</DelTel>
            <DelFax></DelFax>
            <AddressType>R</AddressType>
            <CustomerType></CustomerType>
            <TutorNo></TutorNo>
            <PaymentRef>WEB201006296</PaymentRef>
            <Payment>14.50</Payment>
            <PaymentType>SR</PaymentType>
            <BankPostingCode>1201</BankPostingCode>
            <CarriageNominalCode>4401</CarriageNominalCode>
      </Orders>
      <Orders>
            <OrderID>WEB201006296</OrderID>
            <Company></Company>
            <DelContactName>Lydia Jarvis</DelContactName>
            <DelCompany></DelCompany>
            <DelAddress1>2 Edward Street</DelAddress1>
            <DelAddress2>Trecynon</DelAddress2>
            <DelCounty>R.C.T</DelCounty>
            <DelPostCode>CF44 8LW</DelPostCode>
            <DelTown>Aberdare</DelTown>
            <DelCountry>GB</DelCountry>
            <OrderDate>2010-06-29 10:45:54</OrderDate>
            <Carriage>1.50</Carriage>
            <CarriageTaxCode>0</CarriageTaxCode>
            <tax>0.00</tax>
            <DelEmail>lyddiej@hotmail.com</DelEmail>
            <DelTel>01685874500</DelTel>
            <DelFax></DelFax>
            <AddressType>R</AddressType>
            <CustomerType></CustomerType>
            <TutorNo></TutorNo>
            <PaymentRef>WEB201006296</PaymentRef>
            <Payment>14.50</Payment>
            <PaymentType>SR</PaymentType>
            <BankPostingCode>1201</BankPostingCode>
            <CarriageNominalCode>4401</CarriageNominalCode>
      </Orders>
      <OrderItems>
            <OrderID>WEB201006293</OrderID>
            <ProductCode>SOC10250W</ProductCode>
            <Description>Hello Mr Tumble! DVD</Description>
            <Quantity>1</Quantity>
            <Price>11.06</Price>
            <TaxCode>1</TaxCode>
      </OrderItems>
      <OrderItems>
            <OrderID>WEB201006295</OrderID>
            <ProductCode>DLR01040W</ProductCode>
            <Description>Pocket book of signs 1</Description>
            <Quantity>1</Quantity>
            <Price>6.5</Price>
            <TaxCode>0</TaxCode>
      </OrderItems>
      <OrderItems>
            <OrderID>WEB201006296</OrderID>
            <ProductCode>DLR02040W</ProductCode>
            <Description>Pocket book of signs 2</Description>
            <Quantity>1</Quantity>
            <Price>6.5</Price>
            <TaxCode>0</TaxCode>
      </OrderItems>
      <OrderItems>
            <OrderID>WEB201006296</OrderID>
            <ProductCode>DLR01040W</ProductCode>
            <Description>Pocket book of signs 1</Description>
            <Quantity>1</Quantity>
            <Price>6.5</Price>
            <TaxCode>0</TaxCode>
      </OrderItems>
</DsOrders>


What I would like to do is loop through the data where the data is between  <Orders> and </Orders>

I would then like to show distinct orders based on <OrderID> </OrderID>

As you can see there are 2 orders with the same OrderID <OrderID>WEB201006296</OrderID>

I only need to process the one Order.

Then I would like to loop through where the data is between the Tags <OrderItems> and </OrderItems>

I am able to import xml file and process all the data but I am stuck.

Here is my code:

 Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.TextBox1.Text = ""

        Dim rd As XmlReader = XmlReader.Create("c:\orderimport2.xml")

        While rd.Read

            Select Case rd.NodeType

                Case XmlNodeType.Element

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<{0}>", rd.Name) & ControlChars.NewLine

                Case XmlNodeType.Text

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format(rd.Value) & ControlChars.NewLine

                Case XmlNodeType.CDATA

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<![CDATA[{0}]]>", rd.Value) & ControlChars.NewLine

                Case XmlNodeType.ProcessingInstruction

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<?{0} {1}?>", rd.Name, rd.Value) & ControlChars.NewLine

                Case XmlNodeType.Comment

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<!--{0}-->", rd.Value) & ControlChars.NewLine

                Case XmlNodeType.XmlDeclaration

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<?xml version='1.0'?>") & ControlChars.NewLine

                Case XmlNodeType.Document

                Case XmlNodeType.DocumentType

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<!DOCTYPE {0} [{1}]", rd.Name, rd.Value) & ControlChars.NewLine

                Case XmlNodeType.EntityReference

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format(rd.Name) & ControlChars.NewLine

                Case XmlNodeType.EndElement

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("</{0}>", rd.Name) & ControlChars.NewLine

            End Select

        End While

        rd.Close()
    End Sub

Any help please




Answer : Loop through XML file using VB.Net

you can use dataset.readxml command to read XML and then you easily fire a loop in a table or dataset or you can have the Select Distinct records from the dataset or datatable
Random Solutions  
 
programming4us programming4us