C# 将数据集转换为嵌套xml???帮助我
我有1个数据集,名为Invoices,共2个datatable,数据取自sql server 2008。忽略数据库中的数据,我将重点放在导出为xml的数据集上。名为Invoice的数据表1包括OrderID、CustomerID、CustomerName、CustomerPhone。 数据表2,命名产品,包括ProductID、OrderID、ProductName、价格、数量、金额。C# 将数据集转换为嵌套xml???帮助我,c#,sql-server-2008,C#,Sql Server 2008,我有1个数据集,名为Invoices,共2个datatable,数据取自sql server 2008。忽略数据库中的数据,我将重点放在导出为xml的数据集上。名为Invoice的数据表1包括OrderID、CustomerID、CustomerName、CustomerPhone。 数据表2,命名产品,包括ProductID、OrderID、ProductName、价格、数量、金额。 我的问题是,我想得到这个输出 <Invoices> <OrderID>1<
我的问题是,我想得到这个输出
<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>
因此,我必须调整代码,以便根据需要导出xml???您的xml无效,因为它包含和和符号。应该是&这个问题可能可以在数据集查询中解决,但是没有足够的关于用于创建xml的方法的信息。您始终可以使用xml linq编辑xml代码以过滤输出:
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List removietems=doc.subjections(“catname”)。其中(x=>(string)x!=“Hotel”).ToList();
removietems.Remove();
}
}
}
您的代码在哪里?你需要包括一个。看看and.a数据集,比如System.Data.dataset
(我想这是对的)。数据集对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;
}
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();
}
}
}