Getting a value out of XML

  • Thread starter Thread starter David Lozzi
  • Start date Start date
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
 
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
 
David,
You can also use XmlDocument to modify the document and do whatever
you want to do.
Patrick
 
Can I have a little more explanation? I tried using the XMLDocument but
couldn't figure out how.

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

Thanks,

David Lozzi
 
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
 
Back
Top