如何使用Foreach迭代集合来构建XDocument?

时间:2010-07-20 09:16:14

标签: c# xml linq foreach linq-to-xml

以下代码给出了错误:

  

最佳重载方法匹配   “System.Xml.Linq.XElement.XElement(System.Xml.Linq.XName,   对象)'有一些无效的参数。

我需要更改哪些内容,以便我可以使用Foreach构建XDocument来遍历我的List<Customer>集合?

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

2 个答案:

答案 0 :(得分: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; }
    }
}

答案 1 :(得分:4)

您必须将customers.ForEach更改为customers.ConvertAll