XML : How to get an attribut ?

O

Olivier P.

Hi,

Just a quiet simple question,
If I have an Xml file like that :

<address>
<name>Mitchel</name>
<phone>1293 xxxxxx</phone>
<town>New York
<dpt z="saturn">something</dpt>
<region>earth</region>
</town
</adress>

How can I do in C# to retrieve the value of the attribut "z" in node "dpt" ?
The aim is simply to use it in a "Label.Text = " commande...

I'm looking for that since two days, but I can't find....

Any help ?

Thanks a lot in advance,
By,
Olivier
 
A

Alex Feinman [MVP]

XmlDocument doc;
// load xml here
string attr = doc["address"]["name"]["town"]["dpt"].GetAttribute("z");
 
O

Olivier P.

Alex Feinman said:
XmlDocument doc;
// load xml here
string attr = doc["address"]["name"]["town"]["dpt"].GetAttribute("z");

Olivier P. said:
Hi,

Just a quiet simple question,
If I have an Xml file like that :

<address>
<name>Mitchel</name>
<phone>1293 xxxxxx</phone>
<town>New York
<dpt z="saturn">something</dpt>
<region>earth</region>
</town
</adress>

How can I do in C# to retrieve the value of the attribut "z" in node
"dpt"
?
The aim is simply to use it in a "Label.Text = " commande...

I'm looking for that since two days, but I can't find....

Any help ?

Thanks a lot in advance,
By,
Olivier

Hi,

Ok, it works perfectly, thanks a lot !
So now can I use the same thing if I have such a file :

<address>
<name>Mitchel</name>
<phone>1293 xxxxxx</phone>
<town id="5">
<dpt z="saturn">something</dpt>
<region>earth</region>
</town>
<town id="6">
<dpt z="pluto">something</dpt>
<region>exoplanet</region>
</town>
</adress>

And I would like to retrieve all "id" attributes... how can I move between
different node, preferently using XmlDocument object as you do in your last
sample ?

Thanks a lot for your help !

Olivier
 

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

Top