【问题标题】:Dataset to nested XML form数据集到嵌套的 XML 表单
【发布时间】:2018-06-10 03:16:23
【问题描述】:

您好,我有如下数据表

分类表

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 的主键和外键创建具有类别和子类别表之间关系的嵌套 XML。还有 Subcategory 和 Subtosubcategory 表,列名 SubId 的主键和外键?

【问题讨论】:

  • 执行 ds.WriteXml(Server.MapPath("~/") + "Product.xml");没有给出预期的输出
  • @santoshsingh 我如何在 2 列之间提供参考

标签: c# asp.net xml dataset xmlnode


【解决方案1】:

首先,您必须使用 Relation 对象在表之间建立关系,然后在数据集上调用 save 重载

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

希望这会有所帮助。

【讨论】:

  • 嗨也试过了,但它没有生成 xml,因为我需要 ds.Relations.Add("CatSubCat", ds.Tables["CategoryTable"].Columns["CatID"], ds.Tables[ "SubCategoryTable"].Columns["CatId"]); ds.Relations.Add("SubCatSubSubCat", ds.Tables["SubCategoryTable"].Columns["SubID"], ds.Tables["SubtoSubCategoryTable"].Columns["SubID"]);
  • 您也可以尝试复制整个代码并尝试生成xml
  • 很好用。我只是忘了放 ds.Relations["CatSubCat"].Nested = true;和 ds.Relations["SubCatSubSubCat"].Nested = true;放
【解决方案2】:

您需要在父/子数据表之间创建DataRelation,并将Nested 属性设置为true

这里有来自MS Docs的详细解释

【讨论】:

    【解决方案3】:

    你可以使用XmlSerializer;

    var writer = new XmlTextWriter(Server.MapPath("~/")+"Product.xml", Encoding.UTF8);
    var xmlSerializer = new XmlSerializer(typeof(DataSet));
    xmlSerializer.Serialize(writer, ds);
    

    【讨论】:

    • 您好,我已经更新了我的答案,您可以复制我的代码并在创建 xml 时签入您自己的系统。我希望它是基于主键和外键的嵌套 xml
    • 您能分享所需的 xml 示例吗?
    • 好吧,我认为您无法将数据集序列化为所需的 xml。执行它的唯一方法是创建模型类并将其序列化而不是数据集。
    • 我会从数据库中获取数据,你能举个例子解释一下吗?我从来没有尝试过这样的事情
    • 不,我使用 asp.net 3 层架构和 ado.net
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多