簡體   English   中英

如何從Windows Phone 8中的多個后代獲取XElement的值

[英]How to get values of XElements from more than one Descendants in Windows Phone 8

如何從多個后代中獲取XElements值,請參閱我的XML文檔:

<?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>

我想要“學生姓名”和“學生ID”,我該如何設置條件? 任何幫助將不勝感激!!

這是代碼:

 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();

如果確定<StudentID>始終跟在<StudentName> ,則可以選擇所有<StudentID>后代,然后使用XElement.NextNode獲取相應的<StudnetName>

list = (from id in doc.Descendants("StudentId")
        select new STUDENT
                    {
                        stdId = (Int64)id,
                        stdName = (string)(XElement)id.NextNode
                    }).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM