0

How do I get XElements values from more than one Descendants See my XML Document:

<?xml version="1.0" encoding="UTF-8"?>
<root>

<WithoutGroup>
   <StudentId>21</StudentId>
   <StudentName>Photo</StudentName>
   <Image>dshdsdhshdsghs</Image>
<WithoutGroup>

<group>
   <groupId>471</groupId>
   <groupName>General </groupName>

     <Student>
       <StudentId>85</StudentId>
       <StudentName>Action</StudentName>
       <Image>qwerxcxcxcbvbxcx</Image>
     </Student>

     <Student>
       <StudentId>27</StudentId>
       <StudentName>Docs</StudentName>
       <Image>xcxncbxncsds</Image>
     </Student>

</group>

</root>

I want "STUDENT NAME" and "STUDENT ID", how do I put condition? Any help would be greatly appreciated!!

Here is code:

 XDocument doc = XDocument.Parse(e.Result);

            List<STUDENT> list = new List<STUDENT>();

            list = (from query in doc.Descendants("WithoutGroup")
                       select new STUDENT
                       {
                           stdId = Convert.ToInt64(query.Element("StudentId").Value),
                           stdName = query.Element("StudentName").Value,
                           Icon = getImage(query.Element("Image").Value),

                       }).ToList();
Nitesh Kothari
  • 890
  • 12
  • 27

1 Answers1

1

If you're sure that <StudentID> always followed by <StudentName>, you can select all <StudentID> descendants and use XElement.NextNode to get corresponding <StudnetName> :

list = (from id in doc.Descendants("StudentId")
        select new STUDENT
                    {
                        stdId = (Int64)id,
                        stdName = (string)(XElement)id.NextNode
                    }).ToList();
har07
  • 88,338
  • 12
  • 84
  • 137