c# - 将多个行值从数据库传递到 XML

标签 c# xml xsd

我想生成以下格式的 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>
      <Sales DocNo = 'S0001'>
      <Item>XXX</Item>
      <Qty>2</Qty>
      <Price>6.00</Price>
      </Sales>
      <Sales DocNo = 'S0002'>
      <Item>YYY</Item>
      <Qty>3</Qty>
      <Price>50.00</Price>
      </Sales>
      </AutoCount> 

我有一个包含四列 Docno、item、qty、price 和两行数据的网格。 但我只得到一个销售节点,其中最后一行数据在网格中。

我试过的代码如下,

        string PATH = "C:\\Samplex.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;
    for (int i = 0; i < dataGridView1.RowCount - 1; i++) 
    {
        var sales = new SalesInvoice();
        data.SalesInvoice = new[] { sales };
        sales.DocNo = dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(); 
        sales.Item = dataGridView1.Rows[i].Cells[1].FormattedValue.ToString();
        sales.Qty = dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(); 
        sales.Price = dataGridView1.Rows[i].Cells[3].FormattedValue.ToString();
        var serializer1 = new XmlSerializer(typeof(SalesInvoice)); 
    }
      var serializer = new XmlSerializer(typeof(AutoCount));
        using (var stream = new StreamWriter(PATH))
        serializer.Serialize(stream, data);

输出是:

      <?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 = 'S0002'>
      <Item>YYY</Item>
      <Qty>3</Qty>
      <Price>50.00</Price>
      </Sales>
      </AutoCount> 

最佳答案

除其他问题外,主要问题在这里:

data.SalesInvoice = new[] { sales };

您在每个循环循环中从头开始创建 SalesInvoice 数组,而不是在循环之前创建它并在其中添加元素。

我会这样做(请也看看变量名):

//Plural as it is a list
List<SalesInvoice> salesInvoices = new List<SalesInvoice>();

for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
    //Singular as it is a single element
    var salesInvoice = new SalesInvoice();
    salesInvoice.DocNo = dataGridView1.Rows[i].Cells[0].FormattedValue.ToString();
    salesInvoice.Item = dataGridView1.Rows[i].Cells[1].FormattedValue.ToString();
    salesInvoice.Qty = dataGridView1.Rows[i].Cells[2].FormattedValue.ToString();
    salesInvoice.Price = dataGridView1.Rows[i].Cells[3].FormattedValue.ToString();
    salesInvoices.Add(salesInvoice);

    //What's the purpose of the next line?
    var serializer1 = new XmlSerializer(typeof(SalesInvoice));
}

// I pluralized this property name too
data.SalesInvoices = salesInvoices.ToArray();

关于c# - 将多个行值从数据库传递到 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30010952/

相关文章:

c# - 防止嵌套对象的 mongodb c# 驱动程序将 id 序列化为 _id

c# 在保存对话框上自定义控件——如何禁用父文件夹按钮?

c# - 写入一个值到PE文件

html - XSL 中空白的奇怪 CSS 问题

maven-2 - 如何告诉 jaxb/Maven 生成多个模式包?

c# - C++ 和 .NET 是否在银行、医疗保健和电信等领域一起使用

java - 可扩展列表 : different background colors for ListGroup Items

asp.net - 基于文件的数据库 asp.net

xml - xsd 从枚举或等效类型中选择多个值

java - 在 Java 中创建带有子节点的 SOAP 消息