gpt4 book ai didi

c# - 在 C# 中将对象数组转换为 XML

转载 作者:太空狗 更新时间:2023-10-29 17:36:06 26 4
gpt4 key购买 nike

我知道没有内置转换器可以将对象数组转换为 XML。有没有一种快速的基本方法可以从数组中创建一个 XML,以帮助我在这个 XML 和我拥有的另一个 XML 之间执行 LINQ to XML 连接?

最佳答案

您可以使用 Linq to XML,将现有数据结构映射到 XML 非常容易,即:

int[] values = { 1, 2, 17, 8 };

XDocument doc = new XDocument();
doc.Add(new XElement("root", values.Select( x=> new XElement("item", x))));

产生以下输出:

<root>
<item>1</item>
<item>2</item>
<item>17</item>
<item>8</item>
</root>

关于c# - 在 C# 中将对象数组转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771500/

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