数据集嵌套 XML 形式
Dataset to nested XML form
您好,我有如下数据table
类别表
CatID CategoryName
1 Name1
2 Name2
3 Name3
4 Name4
子类别表
SubId SubCatName CatId
1 SubName1 1
2 SubName2 1
3 SubName3 1
4 SubName4 2
5 SubName5 2
6 SubName6 3
子类别
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 的主键和外键创建在类别和子类别 table 之间具有关系的嵌套 XML。还有 Subcategory 和 Subtosubcategory table 以及列名 SubId 的主键和外键?
您可以使用 XmlSerializer
;
var writer = new XmlTextWriter(Server.MapPath("~/")+"Product.xml", Encoding.UTF8);
var xmlSerializer = new XmlSerializer(typeof(DataSet));
xmlSerializer.Serialize(writer, ds);
您需要在 Parent/Child 个数据表之间创建 DataRelation
,并将 Nested
属性 设置为 true
。
下面是MS Docs
的详细解释
首先,您必须使用 Relation 对象在表之间建立关系,然后在数据集上调用保存重载
ds.Tables.Add(SubtoSubCategoryTable);
//add relations to the tables here
ds.Relations.Add("CatSubCat", CategoryTable.Columns["CatID"], SubCategoryTable.Columns["CatID"]);
ds.Relations["CatSubCat"].Nested = true;
ds.Relations.Add("SubCatSubSubCat", SubCategoryTable.Columns["SubID"], SubtoSubCategoryTable.Columns["SubID"]);
ds.Relations["SubCatSubSubCat"].Nested = true;
//call the writexml overload with WriteSchema to save the contents with schema
ds.WriteXml(Server.MapPath("~/") + "Product.xml", XmlWriteMode.WriteSchema);
更新:
进行了一些更正并在 https://dotnetfiddle.net/31B2Bg
发布了样本
希望对您有所帮助。
您好,我有如下数据table
类别表
CatID CategoryName
1 Name1
2 Name2
3 Name3
4 Name4
子类别表
SubId SubCatName CatId
1 SubName1 1
2 SubName2 1
3 SubName3 1
4 SubName4 2
5 SubName5 2
6 SubName6 3
子类别
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 的主键和外键创建在类别和子类别 table 之间具有关系的嵌套 XML。还有 Subcategory 和 Subtosubcategory table 以及列名 SubId 的主键和外键?
您可以使用 XmlSerializer
;
var writer = new XmlTextWriter(Server.MapPath("~/")+"Product.xml", Encoding.UTF8);
var xmlSerializer = new XmlSerializer(typeof(DataSet));
xmlSerializer.Serialize(writer, ds);
您需要在 Parent/Child 个数据表之间创建 DataRelation
,并将 Nested
属性 设置为 true
。
下面是MS Docs
的详细解释首先,您必须使用 Relation 对象在表之间建立关系,然后在数据集上调用保存重载
ds.Tables.Add(SubtoSubCategoryTable);
//add relations to the tables here
ds.Relations.Add("CatSubCat", CategoryTable.Columns["CatID"], SubCategoryTable.Columns["CatID"]);
ds.Relations["CatSubCat"].Nested = true;
ds.Relations.Add("SubCatSubSubCat", SubCategoryTable.Columns["SubID"], SubtoSubCategoryTable.Columns["SubID"]);
ds.Relations["SubCatSubSubCat"].Nested = true;
//call the writexml overload with WriteSchema to save the contents with schema
ds.WriteXml(Server.MapPath("~/") + "Product.xml", XmlWriteMode.WriteSchema);
更新:
进行了一些更正并在 https://dotnetfiddle.net/31B2Bg
发布了样本希望对您有所帮助。