我有一个xsd文件acount.xsd,使用它创建了acount.cs文件,并使用它创建了xml文件。我已经在c#中编写了以下命令,用于单击按钮以生成xml文件。
string PATH = "C:\\Sample.xml";
CreateEmptyFile(PATH);
var data = new AutoCount();
data.Product = "AutoCount Accounting";
data.Version = "1.5";
data.CreatedApplication = "BApp";
data.CreatedBy = "Business Solutions";
data.CreatedDateTime = DateTime.Now;
var serializer = new XmlSerializer(typeof(AutoCount));
using (var stream = new StreamWriter(PATH))
serializer.Serialize(stream, data);
输出XML文件为:
<?xml version="1.0" encoding="utf-8"?>
<AutoCount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.autocountsoft.com/ac_accounting.xsd">
<Product>AutoCount Accounting</Product>
<Version>1.5</Version>
<CreatedApplication>BApp</CreatedApplication>
<CreatedBy>Business Solutions</CreatedBy>
</AutoCount>
我需要一个具有子类的xml文件,例如Output XML file应该是:
<?xml version="1.0" encoding="utf-8"?>
<AutoCount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.autocountsoft.com/ac_accounting.xsd">
<Product>AutoCount Accounting</Product>
<Version>1.5</Version>
<CreatedApplication>BApp</CreatedApplication>
<CreatedBy>Business Solutions</CreatedBy>
<Sales DocNo = 'S0001'>
<Item>XXX</Item>
<Qty>2</Qty>
<Price>6.00</Price>
</Sales>
</AutoCount>
为了实现这一点,我尝试了以下C#命令,但发生错误(已指定错误)如何实现上述结果?
string PATH = "C:\\Sample.xml";
CreateEmptyFile(PATH);
var sales = new SalesInvoice();
sales.DocNo = "S0001";
sales.Item = "XXX";
sales.Qty= "2";
sales.Price= "6.00";
var data = new AutoCount();
data.Product = "AutoCount Accounting";
data.Version = "1.5";
data.CreatedApplication = "BApp";
data.CreatedBy = "Business Solutions";
data.CreatedDateTime = DateTime.Now;
data.SalesInvoice = sales; /*Error: Cannot implicitly convert SalesInvoice to SalesInvoice[] */
var serializer = new XmlSerializer(typeof(AutoCount));
using (var stream = new StreamWriter(PATH))
serializer.Serialize(stream, data);
尝试这个:
data.SalesInvoice = new [] { sales };
如错误消息所述,您需要提供一个SalesInvoice对象数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句