dataset.relations属性
前几天想写个树形的XML,知道是用dataset.getxml()方法来实现
网上查了一些资料,查了几本书,想了好久终于搞定了!
下面就是我写的生成树形的XML:
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using BLL;
12
13 public partial class Mytest : System.Web.UI.Page
14 {
15 private string singername;
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 singername = Request.QueryString["singer"]; //URL输入singer
19 singername = "周杰伦"; //这里为了方便我直接用姓名带入
20 DataSet ds = new DataSet("singertitle"); //新建一个dataset
21 DataTable singer = new DataTable(); // 新建一个datatable来放歌手
22 DataTable title = new DataTable(); //新建一个datatable来放歌手的专辑
23 BllSinger bllsinger = new BllSinger(); //BLL层
24 BllTitle blltitle = new BllTitle();
25 singer = bllsinger.GetSinger(singername); //给singer 填充数据
26 title = blltitle.GetTitle(singername); //给title填充数据
27 singer.TableName = "Siner"; //重命名表名,要不是默认的table1
28 title.TableName = "Title"; //同上;
29
30 ds.Tables.Add(singer); //把表填充到dataset里
31 ds.Tables.Add(title); //同上
32
33 ds.Relations.Add(new DataRelation("singertitle", singer.Columns["singerid"],
34 title.Columns["singerid"])); ///建立表间关系 singer的singerid 和title的singerid
35 ds.Relations[0].Nested = true; //把结构嵌套在dataset里,这个很重要,我就因为这个搞了好久
36
37 int titlecount; //用来获取歌手有几个title
38 titlecount = blltitle.GetCounttitle(singername); //给titlecount赋值
39 DataTable []song = new DataTable[titlecount]; //创建一个datatable数组用来放专辑的(每个专辑一个datatable)
40 for (int i = 0; i < titlecount; i++)
41 {
42 BllSong bllsong = new BllSong();
43 song[i] = bllsong.MyGetSongInfro(title.Rows[i]["专辑名称"].ToString()); //给专辑的datatable填充数据
44 ds.Tables.Add(song[i]); //添加到dataset里
45 song[i].TableName = "title"+i+"song";
46 ds.Relations.Add(new DataRelation("titletosong"+i, title.Columns["titleid"], song[i].Columns["titleid"]));
47 //建立表间关系
48 ds.Relations[i+1].Nested = true; //把结构嵌套在dataset里
49
50 }
51
52 Response.Write(ds.GetXml()); //输出XML文件
53 }
54 }
55
56 由singer得到singer的信息
64
65 由singer获取title的信息
73
74 由titlename获取专辑里的歌曲
82
这样就生成了一个树形的xml页
本文所有权归作者,欢迎转载,但是请指明出处 :http://www.cnblogs.com/fly871117 ,严谨用于商业用途