Extracting info from an XML file

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
Folks,

I need to extract some information from an XML file - specifically a number between the tags:

<fx-rate>1.23456789</fx-rate>

Does anyone know how to do this exactly?

Cheers,
G.
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
VB.NET:
dim xmldoc as new xmldocument()
xmldoc.load("your xml file")
dim nodelist as xmlnodelist
dim nodes as xmlnode
nodelist=xmlnoc.getelementsbytag("fx-rate")
for each nodes in nodelist
messagebox.show(node.innertext)
next
TPM
 

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
Guys - thanks for th comments - much appreciated. Unfortunately the XML file is more complex than originally anticipated. It gives a base currency and a foreign currency and also specifies 3 ranges. check this out:

<base ="eur">
<foreign="gbp">
<base-value min="0.000" max="99.999">
<fx-rate>0.123</fx-rate>
<base-value min="100.000" max="1000.000">
<fx-rate>0.456</fx-rate>
<base-value min="1000.001" max="999999999999999.999">
<fx-rate>0.789</fx-rate>
...etc...

So I'll have to extract each base and foreign value. Compare to my 2 originals. If they match then I have to extract the ranges and compare to my value. Depending ion what this is I the extract the correct fx value.
Sound fairly mucky doesn't it?

Comments/Suggestions anyone?
Cheers,
G.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
The text you have shown is not a well formatted XML file as far I I know.
When I try to open this with any XML viewer, it gives errors.
First, <base ="eur"> is incorrect. For a well formatted file it should be something like <base name="eur">.
Also the base-value nodes aren't ended (</base-value>)
 

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
Of course - your right - I was just paraphrasing and thinking out loud. Do you wwant the exact XML...
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
You could use the ReadXML function of a dataset to fill a dataset with the XML file.
For me it's easier to manipulate a dataSet.

The reason I pointed out the formatting was that the ReadXML function will error if the XML is not well formatted.
 

Administrator

VB.NET Forum Admin
Joined
Jun 3, 2004
Messages
1,461
Programming Experience
10+
Look at using an XMLTextReader and the MoveToAttribute Methods to get the data. It's not hard and very easy to use, thanks to .NET! :)
 

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
Neal,

Can you give me some sample code?

Cheers
G.
 

Administrator

VB.NET Forum Admin
Joined
Jun 3, 2004
Messages
1,461
Programming Experience
10+
G.

Search for XMLTextReader in your MSDN library or on msdn.microsoft.com and you'll find sample code that is straight forward.
 

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
I have tags in the form:

<foreign iso-alpha="GBP">
<break min="0.000" max="100.00" rate="0.667843">
<break min="100.001" max="9999.999" rate="0.683435>

How the hell do I extract these?

Confused,
G.
 
Last edited:
Top Bottom