[英]convert dataset into nested xml???Help me
我有 1 個數據集,名為 Invoices,有 2 個數據表,數據取自 sql server 2008。忽略數據庫中的數據,我專注於導出到 xml 的數據集。 數據表1,名為Invoice,包括OrderID、CustomerID、CustomerName、CustomerPhone。 數據表 2,名為 Products,包括 ProductID、OrderID、ProductName、Price、Quantity、Amount。
我的問題是,我想得到這個輸出,
<Invoices>
<OrderID>1</OrderID>
<Invoice>
<CustomerID>1</CustomerID>
<CustomerName>A</CustomerName>
<CustomerPhone>123</CustomerPhone>
<Products>
<Product>
<ProductID>1</ProductID>
<ProductName>C</ProductName>
<Price>10</Price>
<Quantity>2</Quantity>
<Amount>20</Amount>
</Product>
</Products>
<TotalAmount>20</TotalAmount> --TotalAmount = sum(amount of products)
</Invoice>
<OrderID>2</OrderID>
<Invoice>
<CustomerID>3</CustomerID>
<CustomerName>D</CustomerName>
<CustomerPhone>1789</CustomerPhone>
<Products>
<Product>
<ProductID>5</ProductID>
<ProductName>V</ProductName>
<Price>30</Price>
<Quantity>3</Quantity>
<Amount>90</Amount>
</Product>
<Product>
<ProductID>9</ProductID>
<ProductName>Z</ProductName>
<Price>5</Price>
<Quantity>4</Quantity>
<Amount>20</Amount>
</Product>
</Products>
<TotalAmount>110</TotalAmount> --TotalAmount = sum(amount of products)
</Invoice>
</Invoices>
但我得到這個輸出:
<Invoices>
<OrderID>1</OrderID>
<Invoice>
<CustomerID>1</CustomerID>
<CustomerName>A</CustomerName>
<CustomerPhone>123</CustomerPhone>
<TotalAmount>20</TotalAmount> --TotalAmount = sum(amount of products)
<Products>
<Product>
<ProductID>1</ProductID>
<ProductName>C</ProductName>
<Price>10</Price>
<Quantity>2</Quantity>
<Amount>20</Amount>
</Product>
</Products>
</Invoice>
<OrderID>2</OrderID>
<Invoice>
<CustomerID>3</CustomerID>
<CustomerName>D</CustomerName>
<CustomerPhone>1789</CustomerPhone>
<TotalAmount>110</TotalAmount> --TotalAmount = sum(amount of products)
<Products>
<Product>
<ProductID>5</ProductID>
<ProductName>V</ProductName>
<Price>30</Price>
<Quantity>3</Quantity>
<Amount>90</Amount>
</Product>
<Product>
<ProductID>9</ProductID>
<ProductName>Z</ProductName>
<Price>5</Price>
<Quantity>4</Quantity>
<Amount>20</Amount>
</Product>
</Products>
</Invoice>
</Invoices>
導出xml的代碼是:
public DataSet LoadInvoices()
{
DataSet ds = new DataSet("Invoices");
DataTable dt1 = LoadInvoice();
dt1.TableName = "Invoice";
DataTable dt3 = LoadProduct();
dt3.TableName = "Product";
ds.Tables.Add(dt1);
ds.Tables.Add(dt3);
DataColumn colDT3 = dt3.Columns[2];
DataColumn colDT21 = dt1.Columns[0];
DataRelation rel2 = new DataRelation("PRODUCT"
, colDT21, colDT3);
rel2.Nested = true;
ds.Relations.Add(rel2);
return ds;
}
public string xmlConvert(DataSet ds)
{
string sXML = "";
sXML = ds.GetXml();
return sXML;
}
因此,我必須調整代碼以根據需要導出xml???
您有無效的 xml,因為它包含和符號。 應該是 & 這個問題可能可以在數據集查詢中解決,但沒有足夠的關於用於創建 xml 的方法的信息。 您始終可以編輯 xml 代碼使用 xml linq 過濾輸出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> removeItems = doc.Descendants("catname").Where(x => (string)x != "Hotel").ToList();
removeItems.Remove();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.