如何使用Foreach遍历集合来构建XDocument?

4
以下代码给我报错:

'System.Xml.Linq.XElement.XElement(System.Xml.Linq.XName,object)'的最佳重载方法匹配存在一些无效参数。

我需要怎么改变才能使用 Foreach 遍历我的 List<Customer> 集合并构建 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();
}

你的代码中缺少一个括号,应该在 ... c=> new XElement(... 周围加上括号。 - ULysses
2个回答

7

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


4

你需要将 customers.ForEach 改为 customers.ConvertAll


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接