0

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.

Clue
  • 119
  • 4
  • 15
  • "result" is not a descendant. Try diary.DescendantsAndSelf. You will get errors inside the loop but that will get you into the loop so you can go from there. – Gene S Sep 10 '12 at 12:04
  • I get a nullpointer when I try it with DescendantsAndSelf. Even when I start from Element("result") in that case. – Clue Sep 10 '12 at 12:09

2 Answers2

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

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

svick
  • 236,525
  • 50
  • 385
  • 514
Arjun Shetty
  • 1,575
  • 1
  • 15
  • 36
  • You are the man, pal! It would be nice though, if you could tell me why my first implementation did not work. – Clue Sep 10 '12 at 12:22
  • Its like your 1st node is "result" pointer is already there now you next get is Descendants of "diaryelement" not the "result". – Arjun Shetty Sep 10 '12 at 12:44
0

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.

Alexander
  • 1,299
  • 9
  • 13