我可以使用变量在ASP.Net c#中用多个值填充xml吗?

时间:2015-06-28 20:04:29

标签: c# sql asp.net sql-server xml

大家好我正在尝试使用SQL Server数据库中的值创建和保存XML。

我的代码基本上创建了一个XML字符串,其中填充了我从多个SQL查询中获取的变量(我需要从许多表中获取数据以完成XML)

我正在使用SqlDataReader来获取变量的值。

由于我的XML有点冗长和复杂,我决定根据查询从几个子字符串中放入最终的XML字符串,基本上就像:

finalxml string = startxml + headerxml + buyerxml + sellerxml + linesxml + summaryxml

我一切都好,直到我到达某一点,结果SQL查询返回了多行。

这是我的代码隐藏:

string orderdetails = "select ProductId, Quantity from [Order] inner join OrderItem on [Order].Id=OrderItem.OrderId where OrderId='" + orderID + "'";
SqlCommand com3 = new SqlCommand(orderdetails, dbConn);

dbConn.Open();

using (SqlDataReader orderitem = com3.ExecuteReader())
    while (orderitem.Read())
    {
            string orderxml = "<Order>" + "<OrderItem>" + orderitem.GetInt32(0).ToString() + "</OrderItem>" + "<OrderItem>" + orderitem.GetInt32(1).ToString() + "</OrderItem>" + "</Order>";
            Label20.Text = orderxml;
            string termid = orderitem.GetInt32(0).ToString();
            string termmenny = orderitem.GetInt32(1).ToString();
    }

    dbConn.Close();
}

protected void Button1_Click(object sender, EventArgs e)
{
    string headerxml = "<Order-Header>" +
                            "<DocumentType> számla </DocumentType>" +
                            "<OrderNumber>" + rendSz + "</OrderNumber>" +   
                            "<OrderDate>"+ datum +"</OrderDate>" +   
                            "<ExpectedDeliverydate> " + szallido + "</ExpectedDeliverydate>"+
                            "<PaymentMethod>"+ fizmod +"</PaymentMethod>"+
                            "<Remarks><![CDATA[" + txtFirstName.Text + "]]></Remarks>"+
                            "<Note><![CDATA[" + txtLastName + "]]></Note>" +   
                            "<PreviousOrderNumber><![CDATA[]]></PreviousOrderNumber>" +
                        "</Order-Header>";
    string vevoxml = "<Buyer>" +
                                "<ILN>435</ILN>" +
                                    "<Name>"+ vezeteknev +" "+ keresztnev +"</Name>" +
                                    "<City>" + vevovaros + "</City>" +
                                    "<ZIP>" + irszam + "</ZIP>" +
                                    "<Address>" + vevocim1 + vevocim2 + "</Address>" +
                                    "<E-mail>" + vevoemail +  "</E-mail>" +
                                    "<Telefon>" + vevotel + "</Telefon>" +
                                    "<Contact>" + "nincs" + "</Contact>" +
                                    "<DeliveryName>" + szallkeresztnev +" "+ szallvezeteknev + "</DeliveryName>" +
                                    "<DeliveryCity>" + szallcimvaros + "</DeliveryCity>" +
                                    "<DeliveryZIP>" + szallcimirszam + "</DeliveryZIP>" +
                                    "<DeliveryAddress>" + szallvevocim1 + szallvevocim2 + "</DeliveryAddress>" +
                                "</Buyer>";

    string eladoxml = "<Seller>" +
                                    "<ILN />" +
                                    "<Name>E-Szoftver Kft.</Name>" +
                                    "<City>Budapest</City>" +
                                    "<ZIP>1195</ZIP>" +
                                    "<Address>Ady Endre út 97-99. F/04.</Address>" +
                                    "<E-mail>laszlo@marsalsoft.hu</E-mail>" +
                                "</Seller>";

    string linexml = "<Line>" +
                               "<Line-Item>" +
                                    "<LineNumber>1</LineNumber>" +
                                    "<EAN />" +
                                    "<SupplierItemCode />" +
                                    "<CustomsCode>5829</CustomsCode>" +
                                    "<ItemDescription><![CDATA[E-Számla szoftver frissítési és jogkövetési díj 1 évre]]></ItemDescription>" +
                                    "<ItemNote><![CDATA[]]></ItemNote>" +
                                    "<VATType>27</VATType>" +
                                    "<PackageType>CU</PackageType>" +
                                    "<OrderedQuantity>1</OrderedQuantity>" +
                                    "<UnitOfMeasure>év</UnitOfMeasure>" +
                            "<OrderedUnitNetPrice>13200</OrderedUnitNetPrice>" +
                                "</Line-Item>" +
                                "</Line>"; 


    string strMyXml = "<?xml version=\"1.0\" encoding=\"windows-1250\"?> " +
                      "<Document-Order>"+
                        headerxml +

                            "<Order-Parties>" +
                                vevoxml +
                                eladoxml +
                            "</Order-Parties>"+
                            "<Order-Lines>" +
                                linexml +
                            "</Order-Lines>" +
                            "<Order-Summary>" +
                                "<TotalLines>1</TotalLines>" +
                                "<TotalOrderedAmount>1</TotalOrderedAmount>" +
                                "<TotalNetPrice>13200</TotalNetPrice>" +
                                "<TotalVat>3564</TotalVat>" +
                                "<TotalGross>16764</TotalGross>" +
                            "</Order-Summary>" +
                     "</Document-Order>";

    XmlDocument xDoc = new XmlDocument();
    xDoc.LoadXml(strMyXml);

    xDoc.Save(Server.MapPath("//orders//szamla.xml"));
}

现在lineXML部分是我的问题,它描述了订单中包含的产品。每个xml描述1个订单,但是1个订单可以包含3个产品,在这种情况下,我需要为每个产品创建一个包含少量元素的行:数量,数量,净价,总价。

是否可以将SqlDataReader和变量添加到字符串中,还是需要使用其他方式,例如从查询结果生成的数据集?

我发现了很多关于从数据集生成xml并使用字符串生成xml的信息,但是它们都没有包含有关此特定情况的信息,从SQL Server生成XML,您需要使用多个查询。

幸运的是我的代码是模块化的,所以如果只能用数据集完成,我不需要触摸xml的其他部分。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我同时找到了解决方案,对于我的xml的orderdetails部分,我使用了数据集,而不是将其写入文件,我将其转换为字符串,并将其添加到主xmlstring:

 string orderdetails = "select Rank() OVER (Order by ProductID) as LineNumber, null as EAN, null as CustomsCode, ProductId as SupplierItemCode, '![CDATA['+Product.Name+']' as ItemDescription, '![CDATA['+Product.ShortDescription+']' as ItemNote, null as VATType, 'CU' as PackageType, Quantity as OrderQuantity, 'darab' as UnitOfMeasure, UnitPriceExclTax as OrderedUnitNetPrice from [Order] inner join OrderItem on [Order].Id=OrderItem.OrderId Inner join Product on OrderItem.ProductId=Product.Id where OrderId='" + orderID + "'";
    SqlCommand com3 = new SqlCommand(orderdetails, dbConn);

    dbConn.Open();

    SqlDataAdapter adapter;
    DataSet ds = new DataSet("Line");

            try
            {

                adapter = new SqlDataAdapter(orderdetails, dbConn);
                adapter.Fill(ds, "Line-Item");
                dbConn.Close();
                StringWriter sw = new StringWriter();
                string result = sw.ToString();

                ds.WriteXml(sw);
                ds.WriteXml(@"E:\mypath\Product.xml");  //saved to file as well just to check the correct scheme
                teststring = sw.ToString();
                prodlabel.Text = teststring;

            }
            catch (Exception ex)
            {
                Label20.Text = ex.ToString();

            }