c# xml linq查询结果格式化

本文关键字:结果 格式化 查询 linq xml | 更新日期: 2023-09-27 18:02:49

我正在尝试将这个查询的结果绑定到一个组合框。

public IEnumerable<object> getGenres()
    {
        var genres = (from item in data.Descendants("genre")
                     select new 
                     {
                         Genre = item.Value
                     }).Distinct();

        return genres.ToArray();
    }

我的xml看起来像这样减去根元素。

    <preformance>
         <venue> Captain Cook Tavern </venue>   
         <date> 30/05/2013 </date>
         <time> 11:00pm </time>
         <band> 
            <name> Cult Disney </name>
            <genre> Punk Rock</genre>
         </band>    
     </preformance>

以这种格式绑定。

{ Genre =  Punk Rock }

对我来说,它看起来像我需要去一个节点更深,以获得实际值,而不是xml标签本身,但我不知道如何做到这一点。

谁能给我指个正确的方向?

谢谢

c# xml linq查询结果格式化

您实际上是在创建一个具有单个名为Genre的成员的匿名类。这会导致额外的嵌套。你可以这样做:

public IEnumerable<string> getGenres()
{
    var genres = (from item in data.Descendants("genre")
                 select item.Value).Distinct();
    return genres.ToArray();
}