本文介绍了若要从数据库传递多个行值到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要生成的fllowing格式的XML文件。
<?XML版本=1.0编码=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\">
<产品与GT; AutoCount会计与LT; /产品与GT;
<版本> 1.5 LT; /版本>
< CreatedApplication> BAPP< / CreatedApplication>
< CreatedBy>企业解决方案和LT; / CreatedBy>
<销售DocNo ='S0001'>
<项目> XXX和LT; /项目>
<数量> 2'; /数量>
<价格> 6.00 LT; /价格>
< /销售GT;
<销售DocNo ='S0002'>
<项目> YYY< /项目>
<&数量→3 LT; /数量>
<价格>&50.00 LT; /价格>
< /销售GT;
< / AutoCount>
我有四列Docno,项目,数量,价格和两行数据的网格。
,但我得到在网格最后一行的数据只有一个销售节点。
代码,我试过有:
字符串PATH =C:\\Samplex.xml
CreateEmptyFile(PATH);
VAR数据=新AutoCount();
data.Product =AutoCount会计学;
data.Version =1.5;
data.CreatedApplication =BAPP
data.CreatedBy =商业解决方案;
data.CreatedDateTime = DateTime.Now;
的for(int i = 0; I< dataGridView1.RowCount - 1;我++)
{
VAR销售=新SalesInvoice();
data.SalesInvoice =新[] {}销售;
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 [Ⅰ] .Cells [3] .FormattedValue.ToString();
变种serializer1 =新的XmlSerializer(typeof运算(SalesInvoice));
}
无功序列化=新的XmlSerializer(typeof运算(AutoCount));
使用(VAR流=新的StreamWriter(PATH))
serializer.Serialize(流数据);
输出是:
<?XML版本=1.0编码=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\">
<产品与GT; AutoCount会计与LT; /产品与GT;
<版本> 1.5 LT; /版本>
< CreatedApplication> BAPP< / CreatedApplication>
< CreatedBy>企业解决方案和LT; / CreatedBy>
<销售DocNo ='S0002'>
<项目> YYY< /项目>
<&数量→3 LT; /数量>
<价格>&50.00 LT; /价格>
< /销售GT;
< / AutoCount>
解决方案
在其他的问题,主要的是在这里:
data.SalesInvoice =新[] {}销售;
您创建的,而不是循环前创建并添加从头在埃维周期循环的SalesInvoice阵列, 。它里面的元素
下面是我会怎么做(看变量名也请):
//复数,因为它是一个列表
名单,LT; SalesInvoice> salesInvoices =新的List< SalesInvoice>();
的for(int i = 0; I< dataGridView1.RowCount - 1;我++)
{
//奇异,因为它是一个单一的元素
变种salesInvoice =新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 [Ⅰ] .Cells [3] .FormattedValue.ToString();
salesInvoices.Add(salesInvoice);
//什么是下一行的目的是什么?
变种serializer1 =新的XmlSerializer(typeof运算(SalesInvoice));
}
//我太$ B $复数这个属性名b data.SalesInvoices = salesInvoices.ToArray();
I want to generate an xml file in the fllowing format.
<?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>
I have a grid with four columns Docno,item,qty,price and two rows of data.but i get only one sales node with last row data in grid.
Code i tried is as follows,
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);
Output was:
<?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>
解决方案
Among other problems, the main one is here:
data.SalesInvoice = new[] { sales };
You create the SalesInvoice array from scratch at evey cycle loop, instead of creating it before the cycle and adding elements inside it.
Here's how I would do (look at variable names too please):
//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();
这篇关于若要从数据库传递多个行值到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!