473,549 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Parsing Help

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

<Customer>
<CustomerNumber >C1000</CustomerNumber>
<CustomerName>D emo 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.Whitespace Handling = WhitespaceHandl ing.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.IsStartEle ment() Then
Exit While
End If

MyCust.Number = xmlr.ReadElemen tString("Custom erNumber")

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!

--
- Mitchell Vincent
Nov 21 '05 #1
9 1201
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>D emo Customer 2</CustomerName>
<CustCredit>
<Amount>2000</Amount>
</CustCredit>
<CustCredit>
<Amount>3000</Amount>
</CustCredit>
</Customer>
<Customer>
<CustomerNumber >C1000</CustomerNumber>
<CustomerName>D emo Customer 1</CustomerName>
</Customer>
</Customers>

[VB.NET]
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim xml As New Xml.XmlDocument
xml.Load("..\Te st.xml")
Dim xmlNode As Xml.XmlNode =
xml.SelectSingl eNode("//Customer[./CustomerName/text()='Demo Customer 2']")
Dim xmlNodes As Xml.XmlNodeList =
xmlNode.SelectN odes("./CustCredit")
MsgBox(xmlNodes .Count)
If xmlNodes.Count > 0 Then
For Each xn As Xml.XmlNode In xmlNodes
MsgBox(xn.First Child.InnerText )
Next
End If
Dim xmlNode1 As Xml.XmlNode =
xml.SelectSingl eNode("//Customer[./CustomerName/text()='Demo Customer 1']")
Dim xmlNodes1 As Xml.XmlNodeList =
xmlNode1.Select Nodes("./CustCredit")
MsgBox(xmlNodes 1.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.

Nov 21 '05 #2
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
Nov 21 '05 #3
Open as dataset item
Nov 21 '05 #4
Cor Ligthert wrote:
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!

--
- Mitchell Vincent
Nov 21 '05 #5
Cor Ligthert wrote:
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
Nov 21 '05 #6
Mitchell.

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

myDataSet.Relat ions.Add("Custo merAmount", _
myDataSet.Table s("Customers"). Columns("Custom erNumber"), _
myDataSet.Table s("CustCredit") .Columns("Custo merNumber"))

Is what you can try
Cor
Nov 21 '05 #7
Cor Ligthert wrote:
Mitchell.

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

myDataSet.Relat ions.Add("Custo merAmount", _
myDataSet.Table s("Customers"). Columns("Custom erNumber"), _
myDataSet.Table s("CustCredit") .Columns("Custo merNumber"))

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!

--
- Mitchell
Nov 21 '05 #8
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
Nov 21 '05 #9
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.Row s.Count - 1
If ds.Customer.Row s(i)("CustomerN umber").ToStrin g = "C1000" Then
Exit For
End If
Next
Dim relationname As String = ds.Relations(0) .RelationName
Dim row() As DataRow = ds.Customer.Row s(0).GetChildRo ws(relationname )
MessageBox.Show (row(0)("Amount ").ToString )
///

I hope this helps,

Cor
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5054
by: Boris Boutillier | last post by:
Hi all, I'm looking for parsing a Verilog file in my python module, is there already such a tool in python (a module in progress) to help instead of doing a duplicate job. And do you know of some generic parsing module in python, in which you give some kind of grammar and callbacks ? Thanks for the help
16
2863
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after...
0
4107
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We have an schema (s1) on an Oracle 9i database with database links pointing to a schema (s2) on another Oracle 9i database.
2
6796
by: John Young | last post by:
I'm trying to parse a directory, but am not sure of the best way of doing it. Preferably using only .net instructions. Can anyone give me an idea of how to do this? Thanks in advance for any help that anyone can give me... John
7
5115
by: Lucas Tam | last post by:
Hi all, Does anyone know of a GOOD example on parsing text with text qualifiers? I am hoping to parse text with variable length delimiters/qualifiers. Also, qualified text could run onto mulitple lines and contain characters like vbcrlf (thus the multiple lines). Anyhow, any help would be appreciated. Thanks!
1
2419
by: yonido | last post by:
hello, my goal is to get patterns out of email files - say "message forwarding" patterns (message forwarded from: xx to: yy subject: zz) now lets say there are tons of these patterns (by gmail, outlook, etc) - and i want to create some rules of how to get them out of the mail's html body. so at first i tried using regular expressions:...
9
4047
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics" portions of Babe Ruth page (http://www.baseballprospectus.com/dt/ruthba01.shtml) and store that info in a CSV file. Also, I would like to do this for...
1
1639
by: syhzaidi | last post by:
How can we do Parsing of Hexdecimel in C# reading string from stream file for eg.. i have a file like.......... 0f 2f 12 2d 3a.......in hexa decimal save in a file.txt and i m reading it from the file....... now i have to convert this in decimal and save in an array.of integers.......i thought it can be achieved through parsing ..means 0f...
3
4366
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
3
2069
by: dimasteg | last post by:
Hi all C. Nead some help with string "on the fly" parsing, how it can be realized ? Any ideas? I got some of my own, but it's interesting to get other points of view . Regards.
0
7548
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7743
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7986
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6074
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5391
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3518
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.