2010-07-20 53 views
4

下面的代碼給我的錯誤:我該如何迭代Foreach中的一個集合才能構建XDocument?

The best overloaded method match for 'System.Xml.Linq.XElement.XElement(System.Xml.Linq.XName, object)' has some invalid arguments.

我有什麼改變,所以我可以通過我的收藏List<Customer>用foreach建設的XDocument迭代?

using System; 
using System.Collections.Generic; 
using System.Xml.Linq; 

namespace test_xml3 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      List<Customer> customers = new List<Customer> { 
       new Customer {FirstName="Jim", LastName="Smith", Age=27}, 
       new Customer {FirstName="Hank", LastName="Moore", Age=28}, 
       new Customer {FirstName="Jay", LastName="Smythe", Age=44} 
      }; 

      Console.WriteLine(BuildXmlWithLINQ(customers)); 
      Console.ReadLine(); 
     } 

     private static string BuildXmlWithLINQ(List<Customer> customers) 
     { 
      XDocument xdoc = new XDocument 
      (
       new XDeclaration("1.0", "utf-8", "yes"), 
       new XElement("customers", 
        customers.ForEach(c => new XElement("customer", 
         new XElement("firstName", c.FirstName), 
         new XElement("lastName", c.LastName) 
        ) 
       ) 
      ); 
      return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString(); 
     } 

    } 

    public class Customer 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public int Age { get; set; } 
    } 
} 

補充:

感謝您的回答,我也想出了這個其中工程:

private static string BuildXmlWithLINQ2(List<Customer> customers) 
{ 
    XDocument xdoc = new XDocument(
     new XDeclaration("1.0", "utf-8", "yes") 
    ); 
    XElement xRoot = new XElement("customers"); 
    xdoc.Add(xRoot); 

    foreach (var customer in customers) 
    { 
     XElement xElement = new XElement("customer", 
      new XElement("firstName", customer.FirstName), 
      new XElement("lastName", customer.LastName), 
      new XElement("age", customer.Age) 
     ); 
     xRoot.Add(xElement); 
    } 
    return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString(); 
} 
+0

你必須在代碼中缺少的支架,左右'... C =>新的XElement(...' – ULysses 2010-07-20 09:31:52

回答

4

你必須改變customers.ForEachcustomers.ConvertAll

6

ForEach返回void,不是對新創建集合的引用。

以下工作


using System; 
using System.Collections.Generic; 
using System.Xml.Linq; 
using System.Linq; 

namespace test_xml3 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      List customers = new List { 
       new Customer {FirstName="Jim", LastName="Smith", Age=27}, 
       new Customer {FirstName="Hank", LastName="Moore", Age=28}, 
       new Customer {FirstName="Jay", LastName="Smythe", Age=44} 
      }; 

      Console.WriteLine(BuildXmlWithLINQ(customers)); 
      Console.ReadLine(); 
     } 

     private static string BuildXmlWithLINQ(List customers) 
     { 
      XDocument xdoc = new XDocument 
      (
       new XDeclaration("1.0", "utf-8", "yes"), 
       new XElement("customers", 
        from c in customers select 
         new XElement("customer", 
          new XElement("firstName", c.FirstName), 
          new XElement("lastName", c.LastName) 
         ) 
       ) 

      ); 
      return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString(); 
     } 

    } 

    public class Customer 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public int Age { get; set; } 
    } 
}