RE: XML Parsing Help

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hi

I think we can use XPath to locate the Node we want and then enumerate its
children node.
Assume we have xml file as below.
[Test.xml]
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer>
<CustomerNumber>C2000</CustomerNumber>
<CustomerName>Demo Customer 2</CustomerName>
<CustCredit>
<Amount>2000</Amount>
</CustCredit>
<CustCredit>
<Amount>3000</Amount>
</CustCredit>
</Customer>
<Customer>
<CustomerNumber>C1000</CustomerNumber>
<CustomerName>Demo Customer 1</CustomerName>
</Customer>
</Customers>

[VB.NET]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim xml As New Xml.XmlDocument
xml.Load("..\Test.xml")
Dim xmlNode As Xml.XmlNode =
xml.SelectSingleNode("//Customer[./CustomerName/text()='Demo Customer 2']")
Dim xmlNodes As Xml.XmlNodeList =
xmlNode.SelectNodes("./CustCredit")
MsgBox(xmlNodes.Count)
If xmlNodes.Count > 0 Then
For Each xn As Xml.XmlNode In xmlNodes
MsgBox(xn.FirstChild.InnerText)
Next
End If
Dim xmlNode1 As Xml.XmlNode =
xml.SelectSingleNode("//Customer[./CustomerName/text()='Demo Customer 1']")
Dim xmlNodes1 As Xml.XmlNodeList =
xmlNode1.SelectNodes("./CustCredit")
MsgBox(xmlNodes1.Count)
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

.


Quantcast