gpt4 book ai didi

c# - 用c#写这个xml

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

以编程方式生成此 xml 并保存到文件的最佳方法是什么?数据源将只是一个 csv 文件(如果它使 xml 编程更容易(在这方面灵活),您可能会建议以另一种方式形成 csv 文件):

business name, address line
Comapny Name 1, 123 Main St.
Company Name 2, 1 Elm St.
Company Name 2, 2 Eml St.

<?xml version="1.0"?>
<ArrayOfBusiness xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Business>
<Name>Company Name 1</Name>
<AddressList>
<Address>
<AddressLine>123 Main St.</AddressLine>
</Address>
</AddressList>
</Business>
<Business>
<Name>Company Name 2</Name>
<AddressList>
<Address>
<AddressLine>1 Elm St.</AddressLine>
</Address>
<Address>
<AddressLine>2 Elm St.</AddressLine>
</Address>
</AddressList>
</Business>
</ArrayOfBusiness>

最佳答案

string path = @"C:\Path\To\Output.xml";
List<Business> list = // set data

using (var streamWriter = new StreamWriter(new FileStream(path, FileMode.Write)))
{
using (var xmlWriter writer = new XmlTextWriter(streamWriter))
{
var serialiser = new XmlSerializer(typeof(List<Business>));
serialiser.Serialize(xmlWriter, list);
}
}

关于c# - 用c#写这个xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4235758/

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