Read particular XML

I have this XML:

<palinsesto>
<giorno label="Mer" data="2014/12/31">
<canale description="Premium Cinema" id="KE">
<prg Pod="N" Nettv="N" orafine="06:30" orainizio="06:00" replica="No" primaTv="No">
<durata duratapixel="30">30</durata>
<tipologia>Type</tipologia>
<titolo>evento iniziato ieri</titolo>
<descrizione>--</descrizione>
<audio sottotitoli="No subtitles" audioType="Mono" doppioAudio="One language">Not used</audio>
<parentalRating>LIBERO DA DIVIETI</parentalRating>
<trafficLight/>
<anno>--</anno>
<paese>--</paese>

and i need to read the value in prg class and palinsesto class, i try in this mode but not work

XDocument doc = XDocument.Parse(e.Result);
    var canal = doc.Descendants(XName.Get("description", "canale")).FirstOrDefault();
    var date = doc.Descendants(XName.Get("data", "giorno")).FirstOrDefault();
    var title = doc.Descendants(XName.Get("titolo", "prg")).FirstOrDefault();

return always error

Jon Skeet
people
quotationmark

It looks like you've misunderstood names, attributes and elements. It looks like you just want something like:

XDocument doc = XDocument.Parse(e.Result);
var root = doc.Root;
var canal = root.Element("canale").Attribute("description").Value;
var date = root.Element("giorno").Attribute("data").Value;
var title = root.Element("titolo").Value;

However:

  • Currently none of your first three elements are closed, which would cause the above to fail; it's not clear what your real XML would look like. You should indent it to show the intended structure.
  • Your date is not represented in the normal way for XML - if you're in control of the XML, it would be better to have a value of 2014-12-31
  • The above code assumes you just want the first element from the root. If that's not the case, you'll need to give us more information

people

See more on this question at Stackoverflow