2013-03-02 147 views
0

我正在使用linq代码解析XML文件。这是我的代码。我想绑定细节和图像列表。将system.collection.generic.list转换为列表

XmlSerializer serializer = new XmlSerializer(typeof(Notchs)); 
     XDocument xmlDoc = XDocument.Parse(dataInXmlFile); 
     Notchs notchs = (Notchs)serializer.Deserialize(xmlDoc.CreateReader()); 

     var query = from l in xmlDoc.Descendants("Category") 
      select new Notch 
      { 
       name = (string)l.Attribute("name").Value, 
       Titles = l.Element("Articles").Elements("article") 
         .Select(s => s.Attribute("title").ToString()) 
         .ToList(), 

       Image = l.Element("Articles").Elements("article").Elements("thumb_image").Elements("image") 
         .Select(x => x.Attribute("url").ToString()).ToList() 
      }; 

     foreach (var result in query) 
     { 
      Console.WriteLine(result.name); 
      foreach (var detail in result.Titles) 
      { 
       Console.WriteLine(detail); 
      } 
     } 

     NotchsList.ItemsSource = query.ToList(); 

我试过这个代码,但我得到了像下面的输出..但我想要的细节和图像列表。

name 

    System.Collection.Generic.List'1[string.system] 

    name 

    System.Collection.Generic.List'1[string.system] 
+0

请给我们一些示例数据(XML)以及您的输出应该如何。 – pescolino 2013-03-02 18:24:57

+0

好吧,我会给你的, – user123 2013-03-04 04:20:49

+0

@ user123:将来请编辑你的问题,而不是发布一个新的。如果你不知道如何编辑阅读[faq](http://stackoverflow.com/faq#howtoask)。 – pescolino 2013-03-04 17:23:44

回答

0

我觉得你

Titles = l.Element("Articles").Elements("article") 
            .Select(s => s.Attribute("title").ToString()) 
            .ToList() 

是返回一个IEnumerable<IEnumerable<String>>。您可能需要执行.SelectMany而不是.Select

+0

我试过这个东西我得到错误像不能隐式转换类型'System.Collections.Generic.List '到'System.Collections.Generic.List ' – user123 2013-03-02 05:35:46