Question

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

Était-ce utile?

La solution

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();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top