嗨我有数据表如下
CategoryTable
CatID CategoryName
1 Name1
2 Name2
3 Name3
4 Name4
SubCategoryTable
SubId SubCatName CatId
1 SubName1 1
2 SubName2 1
3 SubName3 1
4 SubName4 2
5 SubName5 2
6 SubName6 3
Subtosubcategory
Id SubCatName SubId
1 S_SubName1 1
2 S_SubName2 1
3 S_SubName3 1
4 S_SubName4 2
5 S_SubName5 2
6 S_SubName6 3
对于一些粗略的想法,我使用以下代码创建了简单的XML表单。
DataTable CategoryTable;
DataTable SubCategoryTable;
DataTable SubtoSubCategoryTable;
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
CategoryTable = new DataTable();
CategoryTable.Columns.Add(new DataColumn("CatID", Type.GetType("System.Int32")));
CategoryTable.Columns.Add(new DataColumn("CategoryName", Type.GetType("System.String")));
fillRows(1, "Name1");
fillRows(2, "Name2");
fillRows(3, "Name3");
fillRows(4, "Name4");
ds.Tables.Add(CategoryTable);
SubCategoryTable = new DataTable();
SubCategoryTable.Columns.Add(new DataColumn("SubId", Type.GetType("System.Int32")));
SubCategoryTable.Columns.Add(new DataColumn("SubCatName", Type.GetType("System.String")));
SubCategoryTable.Columns.Add(new DataColumn("CatId", Type.GetType("System.Int32")));
fillRows1(1, "SubName1", 1);
fillRows1(2, "SubName2", 1);
fillRows1(3, "SubName3", 1);
fillRows1(4, "SubName4", 2);
fillRows1(5, "SubName5", 2);
fillRows1(6, "SubName6", 3);
ds.Tables.Add(SubCategoryTable);
SubtoSubCategoryTable = new DataTable();
SubtoSubCategoryTable.Columns.Add(new DataColumn("Id", Type.GetType("System.Int32")));
SubtoSubCategoryTable.Columns.Add(new DataColumn("SubCatName", Type.GetType("System.String")));
SubtoSubCategoryTable.Columns.Add(new DataColumn("SubId", Type.GetType("System.Int32")));
fillRows2(1, "S_SubName1", 1);
fillRows2(2, "S_SubName2", 1);
fillRows2(3, "S_SubName3", 1);
fillRows2(4, "S_SubName4", 2);
fillRows2(5, "S_SubName5", 2);
fillRows2(6, "S_SubName6", 3);
ds.Tables.Add(SubtoSubCategoryTable);
ds.WriteXml(Server.MapPath("~/") + "Product.xml");
}
private void fillRows(int CatID, string CategoryName)
{
DataRow dr;
dr = CategoryTable.NewRow();
dr["CatID"] = CatID;
dr["CategoryName"] = CategoryName;
CategoryTable.Rows.Add(dr);
}
private void fillRows1(int SubId, string SubCatName, int CatId)
{
DataRow dr;
dr = SubCategoryTable.NewRow();
dr["SubId"] = SubId;
dr["SubCatName"] = SubCatName;
dr["CatId"] = CatId;
SubCategoryTable.Rows.Add(dr);
}
private void fillRows2(int Id, string SubCatName, int SubId)
{
DataRow dr;
dr = SubtoSubCategoryTable.NewRow();
dr["Id"] = Id;
dr["SubCatName"] = SubCatName;
dr["SubId"] = SubId;
SubtoSubCategoryTable.Rows.Add(dr);
}
Xml应该看起来像
<Category>
<CatID>1</CatID>
<CategoryName>Name1</CategoryName>
<SubCategory>
<SubId>1</SubId>
<SubCatName>SubName1</SubCatName>
<Subtosubcategory>
<Id>1</Id>
<SubCatName>S_SubName1</SubCatName>
</Subtosubcategory>
<Subtosubcategory>
<Id>2</Id>
<SubCatName>S_SubName2</SubCatName>
</Subtosubcategory>
<Subtosubcategory>
<Id>3</Id>
<SubCatName>S_SubName3</SubCatName>
</Subtosubcategory>
</SubCategory>
<SubCategory>
<SubId>2</SubId>
<SubCatName>SubName2</SubCatName>
</SubCategory>
<SubCategory>
<SubId>3</SubId>
<SubCatName>SubName3</SubCatName>
</SubCategory>
</Employee>
</Category>
如何使用列名CatId的主键和外键创建具有Category和Subcategory表之间关系的嵌套XML。还有Subcategory和Subtosubcategory表,列名为SubId的主键和外键?
答案 0 :(得分:2)
首先,您必须使用Relation对象在表之间建立关系,然后在数据集
上调用save overload formatter: function() {
var result = '<b>' + Highcharts.dateFormat('%A, %b %e, %Y', this.x) + '</b>';
for (var i = 0; i < this.points.length; i++) {
var datum = this.points[i];
console.log(datum);
result += '<br />Original price: $' + datum.point.high.toFixed(2);
result += '<br />Dropped price: $' + datum.point.low.toFixed(2);
result += '<br />Daily volume: ' + datum.point.name;
}
return result;
}
<强>更新强>
进行了一些更正,并在https://dotnetfiddle.net/31B2Bg
发布了样本希望这有帮助。
答案 1 :(得分:1)
您需要在DataRelation
属性设置为Nested
的父/子数据表之间创建true
。
以下是MS Docs
的详细说明答案 2 :(得分:0)
您可以使用XmlSerializer
;
var writer = new XmlTextWriter(Server.MapPath("~/")+"Product.xml", Encoding.UTF8);
var xmlSerializer = new XmlSerializer(typeof(DataSet));
xmlSerializer.Serialize(writer, ds);