1

I have and XML like this:

<album>
  <image size="small">http://exaplem/example.jpg</image>
  <image size="medium">http://exaplem/example.jpg</image>
  <image size="large"> http://userserve-ak.last.fm/serve/174s/42566323.png </image>
  <image size="extralarge"> http://exaplem/example.jpg </image>
</album>

...and I want to extract and save <image size="large">...</image> as string.

My goal is obtaining the child text node of the extracted element. For example http://userserve-ak.last.fm/serve/174s/42566323.png.

I've tried with

XmlNodeList xnList = xml.SelectNodes("image[@size='large']");
foreach (XmlNode xn in xnList)
{
    .....
}

... but I'm lost.

What's the best way to do what I require to do?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Gonzalo Hernandez
  • 727
  • 2
  • 9
  • 25

1 Answers1

2

It's better to use LINQ 2 XML:

Assuming you have following xml document:

</album>
  <image size="small">http://exaplem/example.jpg</image>
  <image size="medium">http://exaplem/example.jpg</image>
  <image size="large"> http://userserve-ak.last.fm/serve/174s/42566323.png </image>
  <image size="extralarge"> http://exaplem/example.jpg </image>
</album>

Try something like this:

var doc = XDocument.Parse(yourDocumentString);
var largeImageUrl = doc.Root.Elements("image").Single(image => image.Attribute("size").Value == "large").Value;
Vitaliy
  • 702
  • 6
  • 19