简体   繁体   English

C#XML Linq查询结果格式

[英]C# xml linq query results formatting

I am trying to the bind the results of this query to a combo box. 我正在尝试将此查询的结果绑定到组合框。

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


        return genres.ToArray();
    }

My xml looks like this minus the root element. 我的xml看起来像这样,减去了root元素。

    <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>

It binds in this format. 它以这种格式绑定。

{ Genre =  Punk Rock }

To me it looks like i need to go one node deeper to get the actual value instead of the the xml tag itself, but i'm not sure how to do that. 对我来说,看起来我需要更深入一个节点来获取实际值,而不是xml标记本身,但是我不确定该怎么做。

Could anyone point me in the right direction please? 有人能指出我正确的方向吗?

Thanks 谢谢

You are actually creating an anonymous class with a single member called Genre. 您实际上是在使用一个称为流派的成员创建一个匿名类。 This causes the additional nesting. 这导致了额外的嵌套。 Instead you can just do this: 相反,您可以执行以下操作:

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

    return genres.ToArray();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM