views:

95

answers:

2

The following code gives me the error:

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

What do I have to change so I can iterate through my List<Customer> collection with Foreach building the 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; }
    }
}

Added:

Thanks for all your answers, I also came up with this which works:

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();
}
+3  A: 

You have to change customers.ForEach to customers.ConvertAll.

onof
@onof: +1, nice
ULysses
+1 also works thanks
Edward Tanguay
+2  A: 

ForEach returns void, not a reference to a newly created collection.

The following works


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

ULysses
+1 works well, thanks
Edward Tanguay