Getting a value out of XML

D

David Lozzi

Howdy,

I'm new to ASP.Net 2.0 and XML in general. I've used XML documents as
datasets plenty but never had to find a specific value in one before. Below
is a sample XML document I'm working with and my code I'm using to pull the
specific email address from the XML document. Is this the best way of doing
this? Is there a better, faster procedure?

<?xml version="1.0" encoding="utf-8" ?>

<root>

<email [email protected] name="David Lozzi"></email>

<email [email protected] name="David J. Lozzi"></email>

</root>


Dim xr As New XmlTextReader(Server.MapPath("~/xml/addresses.xml"))

xr.ReadToDescendant("root")

Do While xr.Read

If xr.HasAttributes Then

If xr.GetAttribute("name").ToString = "David Lozzi" Then

Response.Write(xr.GetAttribute("address"))

End If

End If

Loop




Thanks,

David Lozzi
 
L

Laurent Bugnion

Hi,

David said:
Howdy,

I'm new to ASP.Net 2.0 and XML in general. I've used XML documents as
datasets plenty but never had to find a specific value in one before. Below
is a sample XML document I'm working with and my code I'm using to pull the
specific email address from the XML document. Is this the best way of doing
this? Is there a better, faster procedure?

<?xml version="1.0" encoding="utf-8" ?>

<root>

<email [email protected] name="David Lozzi"></email>

<email [email protected] name="David J. Lozzi"></email>

</root>

<snip>

Can you modify the XML code? If yes, you could add an ID to your email
tag, the load the document and use GetElementById instead of the loop.

Note that your XML is not well formed: attributes should be enclosed in
"", and empty tags should be put in the form <... /> instead of <...></...>

HTH,
Laurent
 
P

Patrick.O.Ige

David,
You can also use XmlDocument to modify the document and do whatever
you want to do.
Patrick
 
D

David Lozzi

Can I have a little more explanation? I tried using the XMLDocument but
couldn't figure out how.

Thanks,
David
 
D

David Lozzi

Thanks for the tips. I can change the name to ID, or does it have to be an
integer?

Thanks,

David Lozzi
 
L

Laurent Bugnion

Hi,

David said:
Thanks for the tips. I can change the name to ID, or does it have to be an
integer?

Thanks,

David Lozzi

The ID can be any string, as long as it is unique throughout the document.

HTH,
Laurent
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top