xml and asp.net

B

bill

Hi All, I posted this originally to an xml group and got no response.
Hoping someone here has some insight.

New to asp.net here. Been combing google since monday trying to
find the right solution. Most of the solutions are good and reflect the

same examples in the .net books I have but xml is sketchy to me and
this isn't you typical real world project.

Here's the scenario. I query an api via a URL and it responds with xml
data which I need to use to prepoulate the fields of a web form. I
haven't been able to get my head around this one. As you can see from
the XML example below, all of the elements I am after (name, address,
phone etc) have the same element name "attr". I have no control over
the xml document so I can't arrange the tags in a more useful manner.
If you look at the xml, I need to assign each value to a corresponding
textfield on a web form. So the value of attr name="mail" which is
(e-mail address removed) must be assign to txtMail.text. So far I have been able to

return all of the values but I have not been successful in linking them

to that childnode (if thats correct) of name, mail, address etc.


Also should mention I can't really use an array solution like attr[1],
attr[2] which will work, but as I said I have no control over the xml
doc and if the positions change my app is hosed.


My deadline is tomorrow so thank you sincerely for any and ALL
suggestions.


BK


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE dsml (View Source for full doctype...)>
- <dnt complete="true">
- <directory-entries>
- <entry id="A0236721002">
- <attr name="name">
<value>John Doe</value>
</attr>
- <attr name="mail">
<value>[email protected]</value>
</attr>
- <attr name="address">
<value>123 Some Street</value>
</attr>
- <attr name="city">
<value>New York</value>
</attr>
- <attr name="state">
<value>NY</value>
</attr>
- <attr name="zip">
<value>10019</value>
</attr>
- <attr name="phone">
<value>(212)555-1212</value>
</attr>
</entry>
</directory-entries>
</dnt>
 
C

Chris Fulstow

Try something like this:

XmlDocument xmlDoc = new XmlDocument();
// ...load XML from your API into xmlDoc
string nodePath =
"/dnt/directory-entries/entry/attr[@name='mail']/value";
txtMail.Text =
xmlDoc.DocumentElement.SelectSingleNode(nodePath).InnerText;
 
P

Paolo

You have to select the attribute:

The xpath string should be something like this (I haven't checked it: use it
just for reference)
"//entry/attr[@name='mail']"

That means: select the 'attr' nodes that have an attribute named name with
the value of 'mail'.

Here you will find some examples:

http://www.w3schools.com/xpath/xpath_syntax.asp


Hope this will help.
Regards.
 
G

Greg Collins [Microsoft MVP]

It sounds like your problem is an XPath one. To get the value of the city, for example, you would use an XPath such as:

"/dnt/directory-entries/entry/attr[@name='city']/value"
 
B

bill

Thank you all.

Chris your solution turned out to be the fix - bless you!
I played with xPath for 3 days and couldn't get it to give me
everything required. I figure either it is because of the structure of
the xml file or just my being a .net newb. Probably the latter of the
two (I miss classic asp).

I soon as I meet my deadline I'll post my solution.

Thanks again!
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top