【问题标题】:Linq To XML: Find XElement by name in XML and get parent attributeLinq To XML:在 XML 中按名称查找 XElement 并获取父属性
【发布时间】:2012-07-30 09:49:44
【问题描述】:

我有这个 XML:

<?xml version='1.0' encoding='UTF-8'?>
<applications>
    <category cat="Player">
        <app>
            <name>4Media Blu Ray Creator</name>
        </app>
    </category>
    <category cat="Burning">
        <app>
            <name>Nero Micro</name>
        </app>
</category>
</applications>

现在我想在 app 名称中搜索并返回包含类别名称(父 cat 属性)的 XApplications 列表。

这是我的 XApplication 类:

class XApplication
{
    public string Name { set; get; }
    public string Category { set; get; }

    public XApplication(string name, string category)
    {
        Name = name;
        Category = category;
    }
}

【问题讨论】:

    标签: c# wpf xml linq


    【解决方案1】:

    这是我的方法:

    XApplication[] appList = (from xapp in applicationXml.Elements("category").Elements("app")
                               where xapp.Element("name").Value.ToLower().Contains(txtSearch.Text.ToLower())
                               select new XApplication
                               {
                                    Name = xapp.Element("name").Value,
                                    Category = xapp.Parent.Attribute("cat").Value
    
                               }).ToArray();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多