如何使用 Linq 解析 xml?
您好,我有 xml 字符串,如下所示,
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <Result>
- <AllItems>
- <Item attribute="123">
<SubItem subAttribute="1" />
<SubItem2 subAttribute2="2" />
</Item>
- <Item attribute="321">
<SubItem subAttribute="3" />
<SubItem2 subAttribute2="4" />
</Item>
</AllItems>
</Result>
我想获取 allitems 元素中的每个项目获取属性值并将它们添加到 Enumerable 类中
Enumerable<Item> allitems = new Enumerable<Item>();
public class Item()
{
public string attribute{get;set;}
public string subattribute{get;set;}
public string subattribute2{get;set;}
}
如何使用 LINQ 来完成此操作?
Hi I have xml string like you can see below
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <Result>
- <AllItems>
- <Item attribute="123">
<SubItem subAttribute="1" />
<SubItem2 subAttribute2="2" />
</Item>
- <Item attribute="321">
<SubItem subAttribute="3" />
<SubItem2 subAttribute2="4" />
</Item>
</AllItems>
</Result>
I would like to get each item in allitems element get attribute values and add them to Enumerable class
Enumerable<Item> allitems = new Enumerable<Item>();
public class Item()
{
public string attribute{get;set;}
public string subattribute{get;set;}
public string subattribute2{get;set;}
}
How it can be done with using LINQ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的示例在 Linq to XML 中如下所示:
Your example would look like this in Linq to XML:
以下内容将直接转化为您的课程:
Here's something that will directly translate into your class:
您有LINQ to XML,您可以使用它通过 LINQ 查询 XML
You have LINQ to XML which you can use to query XML with LINQ