如何从多个后代获取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>
我想&#34;学生姓名&#34;和&#34;学生ID&#34;,我该如何处理条件? 任何帮助将不胜感激!!
这是代码:
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();
答案 0 :(得分:1)
如果您确定<StudentID>
始终跟随<StudentName>
,则可以选择所有<StudentID>
个后代,并使用XElement.NextNode
获取相应的<StudnetName>
:< / p>
list = (from id in doc.Descendants("StudentId")
select new STUDENT
{
stdId = (Int64)id,
stdName = (string)(XElement)id.NextNode
}).ToList();