[英]How to Parse multiple DESCENDANTS in windows phone 8.1
I am trying to parse multiple Descendants but do not know how to parse. 我正在尝试解析多个后代,但不知道如何解析。
See my XML Document: 查看我的XML文档:
<root>
<withoutGroup>
<Id>123</tabId>
<Name>XYZ</tabName>
<Url>ABC.htm</tabUrl>
<Icon/>
</withoutGroup>
<group>
<groupId>789</groupId>
<groupName>TEST</groupName>
<row>
<Id>345</Id>
<Name>PQR</Name>
<Url>QWE.htm</Url>
<Icon/>
</row>
</group>
</root>
I want to Parse these Nodes: 我想解析这些节点:
Id ID
Name 名称
Url 网址
Icon 图标
How to deal with it? 怎么处理呢?
XDocument doc = XDocument.Parse(e.Result);
List<LIST> list = new List<LIST>();
list = (from query in doc.Descendants("group")
select new LIST
{
Id = Convert.ToInt64(query.Element("Id").Value),
Name = query.Element("Name").Value
}).ToList();
e.Result is my XML Document e.Result是我的XML文档
If I understand this correctly, you want to select Id, Name, Url, and Icon from various parent element name ( row
and withoutGroup
elements in this particular case). 如果我正确理解这一点,则需要从各种父元素名称(在这种情况下为row
和withoutGroup
元素)中选择Id,Name,Url和Icon。 This is one possible way : 这是一种可能的方式:
.......
//list all possible parent elements
var parentElements = new[] {"row", "withoutGroup"};
//get descendants having element name listed in parentElements variable
list = (from query in doc.Descendants()
.Where(o => parentElements.Contains(o.Name.LocalName))
select new LIST
{
Id = (long)query.Element("Id"),
Name = (string)query.Element("Name"),
Url = (string)query.Element("Url"),
Icon = (string)query.Element("Icon"),
}).ToList();
BTW, you can cast XElement
directly to one of types listed in Operator section, as shown in above example. 顺便说一句,您可以将XElement
直接转换为“ 运算符”部分列出的类型之一,如上面的示例所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.