2009-04-30 19 views
2

xmlのすべての情報を配列に格納する必要があります。私は常にXMLから最初の項目を取得するので、私のコードは機能しません。C#LINQ - XMLを読む

誰でもこれを解決する方法を知っていますか? - しかし、あなたはすべての<item>...</item>の要素をループするつもりのように見える - 上記のものです

var items = from item in xdoc.Descendants("item") 
       select new 
       { 
        Title = item.Element("title").Value, 
        // *** NOTE: xml has "desc", not "description" 
        Description = item.Element("desc").Value 
       }; 

それは、サンプルXMLせずに必ずすることは困難少しです:についてどのように

  XDocument xdoc = XDocument.Load("http://www.thefaxx.de/xml/nano.xml"); 
     var items = from item in xdoc.Descendants("items") 
        select new 
        { 
         Title = item.Element("item").Element("title").Value, 
         Description = item.Element("item").Element("description").Value 
        }; 

     foreach (var item in items) 
     { 
      listView1.Items.Add(item.Title); 
     } 
+0

XMLの外観はどうですか? –

答えて

4

そうです。元のコードは、(単一?)<items>...</items>要素をループし、その中から最初の<item>...</item>を取得します。


編集後のxml;これはより効率的です:

var items = from item in xdoc.Root.Elements("item") 
       select new { 
        Title = item.Element("title").Value, 
        Description = item.Element("desc").Value 
       };