Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPATH Childnodes Headache

Status
Not open for further replies.

klornpallier

Technical User
Aug 28, 2002
98
GB
Hi,

PLease help as I've spent two days confused on this! I'm trying the read the xml file below node by node with the code also below this. All the childnodes values seem to be concatinated together on the output, for example:

DeliveryAddress : HO/A0221Wyatt HouseThe ButtsSanctuary GroupWorcesterWorcestershireWR1 3BH

But I want like the below for example:
AddressCode: HO/A0221
Address1 :Wyatt House

=====================

<?xml version="1.0" ?>
- <Orders>
- <Order>
<OrderNo>501238</OrderNo>
<Company>SFA</Company>
<OrderedBy>Meg Hearne</OrderedBy>
<DateOrdered>2010-08-04</DateOrdered>
<OrderDeliveryBy>mail</OrderDeliveryBy>
- <Supplier>
<SuppAcctCode>RARG01</SuppAcctCode>
<SuppAddrCode>RARG01</SuppAddrCode>
</Supplier>
- <OrderItems>
- <OrderItem>
- <RequisitionRef>
- <Doc>
<DocNo>502031</DocNo>
<DocLineNo>1</DocLineNo>
</Doc>
</RequisitionRef>
<Reference />
<DeliveryAddressCode>HO/A0221</DeliveryAddressCode>
- <DeliveryAddress>
<AddressCode>HO/A0221</AddressCode>
<Address1>Wyatt House</Address1>
<Address2>The Butts</Address2>
<Address3 />
<Company>Sanctuary Group</Company>
<TradingAs />
<Suburb>Worcester</Suburb>
<State>Worcestershire</State>
<PostCode>WR1 3BH</PostCode>
<Country />
</DeliveryAddress>
<DeliveryDate>2010-08-31</DeliveryDate>
<ItemCode>CAP101178</ItemCode>
<Description>Klorn Security minor Remodelling</Description>
<SuppItemCode />
<IsReceiptRequired>True</IsReceiptRequired>
<GoodsOrService>goods</GoodsOrService>

=====================

Dim xpathDoc As XPathDocument
Dim xmlNav As XPathNavigator

Dim ReqNo As XPathNodeIterator
xpathDoc = New XPathDocument("\\dev-ipos-01\support\XML Testing\Input\Purchase Orders\Pre Load\ORD-SFA-501238.xml")
xmlNav = xpathDoc.CreateNavigator()
ReqNo = xmlNav.Select("//Orders/Order/OrderItems/OrderItem/*")

While (ReqNo.MoveNext())
System.Console.WriteLine(ReqNo.Current.Name + " : " + ReqNo.Current.Value & " ")

End While
 
[0] No only DeliveryAddress, if you care to look, it would be same for RequisitionRef that would cause more problem as it would be a number being displayed totally wrong...

[1] Under the assumptions that you won't be interested in displaying the intermediate tags' name and that all your xml is of no mixed content type, you can simplify the task by displaying only the element name of that captured by the first path and then displays the immediate element name containing the text for clarity and I suppose it would serve a good purpose too, you can do this.
[tt]
[blue]Dim Niter as XPathNodeIterator[/blue]
While (ReqNo.MoveNext())
[red]'[/red]System.Console.WriteLine(ReqNo.Current.Name + " : " + ReqNo.Current.Value & " ")
[blue]System.Console.Write(ReqNo.Current.Name + " : ")
If (ReqNo.Current.Select(".//*").Count<>0) Then
System.Console.WriteLine()
Niter=ReqNo.Current.Select(".//*[count(child::*)=0]")
While (Niter.MoveNext())
System.Console.WriteLine(chr(9)+Niter.Current.Name+":"+Niter.Current.Value & " ")
End While
Else
System.Console.WriteLine(ReqNo.Current.Value)
End If[/blue]
End While
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top