Microsoft
Software
Hardware
Network
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/DsOrde
rInfo.xsd
"
>
<Orders>
<OrderID>WEB201006293</Ord
erID>
<Company></Company>
<DelContactName>maria barrett</DelContactName>
<DelCompany></DelCompany>
<DelAddress1>23 buttermere close</DelAddress1>
<DelAddress2></DelAddress2
>
<DelCounty>kent</DelCounty
>
<DelPostCode>ME7 2TS</DelPostCode>
<DelTown>gillingham</DelTo
wn>
<DelCountry>GB</DelCountry
>
<OrderDate>2010-06-29 09:25:40</OrderDate>
<Carriage>1.2766</Carriage
>
<CarriageTaxCode>1</Carria
geTaxCode>
<tax>1.93</tax>
<DelEmail>barrettmaria@hot
mail.com</
DelEmail>
<DelTel>07800937785</DelTe
l>
<DelFax></DelFax>
<AddressType>R</AddressTyp
e>
<CustomerType>Personal use</CustomerType>
<TutorNo></TutorNo>
<PaymentRef>WEB201006293</
PaymentRef
>
<Payment>14.49</Payment>
<PaymentType>SR</PaymentTy
pe>
<BankPostingCode>1201</Ban
kPostingCo
de>
<CarriageNominalCode>4401<
/CarriageN
ominalCode
>
</Orders>
<Orders>
<OrderID>WEB201006295</Ord
erID>
<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</Carria
geTaxCode>
<tax>0.00</tax>
<DelEmail>
[email protected]
<
/DelEmail>
<DelTel>07989107285</DelTe
l>
<DelFax></DelFax>
<AddressType>R</AddressTyp
e>
<CustomerType></CustomerTy
pe>
<TutorNo></TutorNo>
<PaymentRef>WEB201006295</
PaymentRef
>
<Payment>8.00</Payment>
<PaymentType>SR</PaymentTy
pe>
<BankPostingCode>1201</Ban
kPostingCo
de>
<CarriageNominalCode>4401<
/CarriageN
ominalCode
>
</Orders>
<Orders>
<OrderID>WEB201006296</Ord
erID>
<Company></Company>
<DelContactName>Lydia Jarvis</DelContactName>
<DelCompany></DelCompany>
<DelAddress1>2 Edward Street</DelAddress1>
<DelAddress2>Trecynon</Del
Address2>
<DelCounty>R.C.T</DelCount
y>
<DelPostCode>CF44 8LW</DelPostCode>
<DelTown>Aberdare</DelTown
>
<DelCountry>GB</DelCountry
>
<OrderDate>2010-06-29 10:45:54</OrderDate>
<Carriage>1.50</Carriage>
<CarriageTaxCode>0</Carria
geTaxCode>
<tax>0.00</tax>
<DelEmail>lyddiej@hotmail.
com</DelEm
ail>
<DelTel>01685874500</DelTe
l>
<DelFax></DelFax>
<AddressType>R</AddressTyp
e>
<CustomerType></CustomerTy
pe>
<TutorNo></TutorNo>
<PaymentRef>WEB201006296</
PaymentRef
>
<Payment>14.50</Payment>
<PaymentType>SR</PaymentTy
pe>
<BankPostingCode>1201</Ban
kPostingCo
de>
<CarriageNominalCode>4401<
/CarriageN
ominalCode
>
</Orders>
<Orders>
<OrderID>WEB201006296</Ord
erID>
<Company></Company>
<DelContactName>Lydia Jarvis</DelContactName>
<DelCompany></DelCompany>
<DelAddress1>2 Edward Street</DelAddress1>
<DelAddress2>Trecynon</Del
Address2>
<DelCounty>R.C.T</DelCount
y>
<DelPostCode>CF44 8LW</DelPostCode>
<DelTown>Aberdare</DelTown
>
<DelCountry>GB</DelCountry
>
<OrderDate>2010-06-29 10:45:54</OrderDate>
<Carriage>1.50</Carriage>
<CarriageTaxCode>0</Carria
geTaxCode>
<tax>0.00</tax>
<DelEmail>lyddiej@hotmail.
com</DelEm
ail>
<DelTel>01685874500</DelTe
l>
<DelFax></DelFax>
<AddressType>R</AddressTyp
e>
<CustomerType></CustomerTy
pe>
<TutorNo></TutorNo>
<PaymentRef>WEB201006296</
PaymentRef
>
<Payment>14.50</Payment>
<PaymentType>SR</PaymentTy
pe>
<BankPostingCode>1201</Ban
kPostingCo
de>
<CarriageNominalCode>4401<
/CarriageN
ominalCode
>
</Orders>
<OrderItems>
<OrderID>WEB201006293</Ord
erID>
<ProductCode>SOC10250W</Pr
oductCode>
<Description>Hello Mr Tumble! DVD</Description>
<Quantity>1</Quantity>
<Price>11.06</Price>
<TaxCode>1</TaxCode>
</OrderItems>
<OrderItems>
<OrderID>WEB201006295</Ord
erID>
<ProductCode>DLR01040W</Pr
oductCode>
<Description>Pocket book of signs 1</Description>
<Quantity>1</Quantity>
<Price>6.5</Price>
<TaxCode>0</TaxCode>
</OrderItems>
<OrderItems>
<OrderID>WEB201006296</Ord
erID>
<ProductCode>DLR02040W</Pr
oductCode>
<Description>Pocket book of signs 2</Description>
<Quantity>1</Quantity>
<Price>6.5</Price>
<TaxCode>0</TaxCode>
</OrderItems>
<OrderItems>
<OrderID>WEB201006296</Ord
erID>
<ProductCode>DLR01040W</Pr
oductCode>
<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</Ord
erID>
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:\order
import2.xm
l")
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.ProcessingInst
ruction
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.EntityReferenc
e
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
Arcserve will not load second tape
windows 7 home password policy
How can I shrink a LDF file of 54GB?
SBS 2003 CAL Licenses
Exchange 2010 Delegate Distribution List Permissions
connectComputer/nshelp.exe
page cannot be found
NETGEAR FVS336G
Google Analytics
syntax error in FROM clause
Exchange 2010 and /exchange error