asp.net - 如何通过 LINQ 获得一级子级

标签 asp.net xml linq

我有这样的XMl

<root>
    <list>
        <list>
            <topic></topic>
            <topic></topic>
        </list>
        <topic></topic>
        <topic></topic>
    </list>
    <topic></topic>
    <topic></topic>
    <topic></topic>
</root>

我需要得到第一级的 child :

<list></list>
<topic></topic>
<topic></topic>
<topic></topic>

我试着这样做

var list = x.Descendants().Where(e => e.Name == "list" || e.Name == "topic");

但它会返回所有主题和列表。

求助! :)

最佳答案

只需 document.Root.Elements() 即可。

基本上Descendants()递归,而 Elements()只得到直接个 child 。

关于asp.net - 如何通过 LINQ 获得一级子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3091586/

相关文章:

c# - 尝试使用 WebClient 下载 URL 中包含 ':' 的文件

c# - "IN"Linq 运算符

c# - Skip and Take 在 Linq to Objects 中的表现

c# - 查询 Linq 从对象返回 List<int>

ASP.NET 图表 : What's wrong with my web. 配置。我无法显示图表

asp.net - 使用 System.Net.Mail 处理 Global.cs 错误

c# - ELMAH:仅通过邮件发送特定的异常类型

java - 添加动画时 Android 应用程序停止

xml - 如何使用 XSLT 将换行符转换为 <br/>?

java - 搜索从 xml 文档创建的 DOM 并将值复制到电子表格/csv