using System.Xml.Serialization;
public static void Main()
static DataTable CategoryTable;
static DataTable SubCategoryTable;
static DataTable SubtoSubCategoryTable;
private static void TestDS()
DataSet ds = new DataSet();
CategoryTable = new DataTable("Category");
CategoryTable.Columns.Add(new DataColumn("CatID", Type.GetType("System.Int32")));
CategoryTable.Columns.Add(new DataColumn("CategoryName", Type.GetType("System.String")));
CategoryTable.PrimaryKey = new DataColumn[] { CategoryTable.Columns["CatID"] };
ds.Tables.Add(CategoryTable);
SubCategoryTable = new DataTable("SubCategory");
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")));
ds.Tables.Add(SubCategoryTable);
SubtoSubCategoryTable = new DataTable("Subtosubcategory");
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")));
ds.Tables.Add(SubtoSubCategoryTable);
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;
fillRows1(1, "SubName1", 1);
fillRows1(2, "SubName2", 1);
fillRows1(3, "SubName3", 1);
fillRows1(4, "SubName4", 2);
fillRows1(5, "SubName5", 2);
fillRows1(6, "SubName6", 3);
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);
Console.WriteLine(ds.GetXml());
private static void fillRows(int CatID, string CategoryName)
dr = CategoryTable.NewRow();
dr["CategoryName"] = CategoryName;
CategoryTable.Rows.Add(dr);
private static void fillRows1(int SubId, string SubCatName, int CatId)
dr = SubCategoryTable.NewRow();
dr["SubCatName"] = SubCatName;
SubCategoryTable.Rows.Add(dr);
private static void fillRows2(int Id, string SubCatName, int SubId)
dr = SubtoSubCategoryTable.NewRow();
dr["SubCatName"] = SubCatName;
SubtoSubCategoryTable.Rows.Add(dr);