简体   繁体   中英

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.

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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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