Clue
Clue

Reputation: 119

XElement iteration won't work

I am trying to go through this XML:

<result>
    <diaryelement>
       <diary_uid>86248040</diary_uid>
       <diary_date>1347274789</diary_date>
       <diary_type>0</diary_type>

       <diaryshortitem>
         <itemid>419</itemid>
         <data>...</data>
          <description>...</description>
       </diaryshortitem>
    </diaryelement>
</result>

and the code I am using for iteration is:

XElement diary = XElement.Parse(e.Result);
IEnumerable<XElement> diaryelements = diary.Descendants("result");

Debug.WriteLine("No error");

                foreach (XElement diaryelement in diaryelements) 
                {
                    Debug.WriteLine(diaryelement.Value);
                    Debug.WriteLine((string)diaryelement.Element("diaryelement").Element("diaryshortitem").Element("description").Element("data").Value);
                }

Debug.WriteLine("Loop ended");

But the method seems to skip the iteration and I don't know why.

Upvotes: 0

Views: 267

Answers (2)

Arjun Shetty
Arjun Shetty

Reputation: 1585

IEnumerable<XElement> diaryelements = diary.Descendants("diaryelement");

This change gets all the Descendants of "diaryelement" after this you can parse one by one

Upvotes: 2

Alexander
Alexander

Reputation: 1319

Instead of XElement.Parse try to use XDocument.Parse. Also, consider adjusting xml elements path on this line

Debug.WriteLine((string)diaryelement.Element("diaryelement").Element("diaryshortitem").Element("description").Element("data").Value);

as it is not correct.

Upvotes: 0

Related Questions