XML Parsing Help

M

Mitchell Vincent

So I have some XML exported from another database. This is greatly
trimmed for brevity's sake..

<Customer>
<CustomerNumber>C1000</CustomerNumber>
<CustomerName>Demo Customer 1</CustomerName>
<CustCredit>
<Amount>1000</Amount>
</CustCredit>
</Customer>

I need to parse those and take into account that there might be an
unknown number of <CustCredit> elements, or none as the case may be.

This is how I'm starting - code idea ripped from some vb.net site :

Dim xmlr As XmlTextReader

'Create the XML Reader
xmlr = New XmlTextReader("export.xml")
xmlr.WhitespaceHandling = WhitespaceHandling.None

'read the xml declaration and advance to family tag
'xmlr.Read()
'read the family tag
'xmlr.Read()

'Load the Loop

While Not xmlr.EOF
'Go to the name tag
'if not start element exit while loop
If Not xmlr.IsStartElement() Then
Exit While
End If

MyCust.Number = xmlr.ReadElementString("CustomerNumber")

End While

'close the reader

xmlr.Close()


It simply doesn't work - or at least not in any way I can see it.

I'd like to have the ability to say "give me this field from the current
<Customer>, now look to see if there is a <CustCredit>, if so, loop
through and give me each of those fields..

Help!!

I have 13 (literally) VB.NET books and none of them give any useful real
world examples of how to read and parse XML!

Thanks!
 
P

Peter Huang [MSFT]

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.
 
C

Cor Ligthert

Mitchel,

Did you already open that XML file inside your IDE it is not impossible it
is a dataset and than is handling very easy.

Cor
 
M

Mitchell Vincent

Cor said:
Open as dataset item

Thanks for the reply Cor, you amaze me how you answer all these posts!

I'll give it a try.

I'm very surprised there isn't some function to do just what I want -
"give me X element". I thought easy (or even possible) data parsing was
what XML is all about!
 
M

Mitchell Vincent

Cor said:
Mitchel,

Did you already open that XML file inside your IDE it is not impossible it
is a dataset and than is handling very easy.

Cor

Ok, so I can get the data into a dataset pretty easily but I don't
understand how to get the CustCredit relation (that may or may not be
there).


Dim ds As DataSet = New DataSet()

ds.ReadXml("my_export.xml")

OK. So. What now? Do I loop through and compare all the names
individually? How do I get the <CustCredit> nested elements that may or
may not be there for each record?

Thanks again Cor..

Yours in frustration,
Mitchell Vincent
 
C

Cor Ligthert

Mitchell.

I don't know it for those nested xml Datasets however something as this is
what you can try.

myDataSet.Relations.Add("CustomerAmount", _
myDataSet.Tables("Customers").Columns("CustomerNumber"), _
myDataSet.Tables("CustCredit").Columns("CustomerNumber"))

Is what you can try


Cor
 
M

Mitchell Vincent

Cor said:
Mitchell.

I don't know it for those nested xml Datasets however something as this is
what you can try.

myDataSet.Relations.Add("CustomerAmount", _
myDataSet.Tables("Customers").Columns("CustomerNumber"), _
myDataSet.Tables("CustCredit").Columns("CustomerNumber"))

Is what you can try


Cor

I'll give that a try..

Can you show me an example of going through the dataset to do something
with each customer's data? This is an import routine and I can't seem to
find any decent tutorials that explain moving through a dataset to get
specific columns..

Thanks Cor!
 
C

Cor Ligthert

Mitchell,

My answer is probably not right, I did not do it this way as I told.

However maybe can you do it this.
Right click your dataset and tell "create schema"
Open that schema from the solution provider
Right click that and tell genereate dataset.

Than there is a class generated. (Strongly typed dataset)
With
dim ds as new (the generated class)
Than you can use that as a normal dataset and do
ds.readxml(the xmlfile)

I hope this helps something

Cor
 
C

Cor Ligthert

Mitchel,

With this code you get than the amount. Probably it can much easier however
I never use the strongly typed dataset in this way.

\\\
Dim ds As New NewDataSet
ds.ReadXml("c:\test1\xmlfile1.xml")
Dim i As Integer
For i = 0 To ds.Customer.Rows.Count - 1
If ds.Customer.Rows(i)("CustomerNumber").ToString = "C1000" Then
Exit For
End If
Next
Dim relationname As String = ds.Relations(0).RelationName
Dim row() As DataRow = ds.Customer.Rows(0).GetChildRows(relationname)
MessageBox.Show(row(0)("Amount").ToString)
///

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top