gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:01:18 24 4
gpt4 key购买 nike

下面的代码给出了错误:

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

最佳答案

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

关于c# - 如何使用 Foreach 遍历集合来构建 XDocument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3288515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com