本代碼從資料庫中讀出記錄,有多層的套用,來產生XML,組建檔案
clientdao.MtvXmlDAO dao=new MtvXmlDAO();
// 在此處放置使用者代碼以初始化頁面
DataTable dtagent=dao.MtvXmlAgent("agent_list").Tables[0];
for(int l=0;l<dtagent.Rows.Count;l++)
{
DataRow dragent=dtagent.Rows[l];
string [] strProKey=new string[1];
strProKey[0]="@agentid";
string [] strProValue=new string[1];
strProValue[0]=dragent["number"].ToString();
DataTable dt=dao.MtvXmlMtv("mtvclassclient_xml",strProKey,strProValue).Tables[0];
DataTable dt2=dao.MtvXmlMtv("mtvclient_xml",strProKey,strProValue).Tables[0];
DataTable dt1=dao.MtvXmlMtvClass("musicclass_list").Tables[0];
string [] type1=new string[5];
type1[0]="大陸";
type1[1]="歐美";
type1[2]="日韓";
type1[3]="港台";
type1[4]="其它";
XmlDocument xd = new XmlDocument();//表示XML文檔
XmlDeclaration xde;//表示 XML 聲明節點:<?xml version='1.0'...?>
xde = xd.CreateXmlDeclaration("1.0", null, null);
xde.Encoding = "gb2312";
xde.Standalone = "yes";
xd.AppendChild(xde);//<?xml version="1.0" encoding="UTF-8" standalone="yes"?>產生結束
XmlElement xe = xd.CreateElement("Root");//建立一個Root根項目
xd.AppendChild(xe);//Root根項目建立完成
XmlNode root = xd.SelectSingleNode("Root");//尋找<Root>
XmlElement kind = xd.CreateElement("kind");//在<Root>之下建立元素<Tree>
for(int p=0;p<5;p++)
{
XmlElement type= xd.CreateElement("type");//在<Root>之下建立元素<Tree>
type.SetAttribute("name", type1[p]);//指定屬性的屬性值
// type.InnerText = "引導1";//指定屬性文本節點
for(int i=0;i<dt.Rows.Count;i++)
{
DataRow dr=dt.Rows[i];
// if(dr["type"].ToString()==type1[p])
// {
string a=dr["mtvclassid"].ToString();
string b="";
string mtvclassid="";
for(int j=0;j<dt1.Rows.Count;j++)
{
DataRow dr1=dt1.Rows[j];
if(a==dr1["id"].ToString())
{
mtvclassid=dr1["id"].ToString();
b=dr1["classname"].ToString();
}
}
XmlElement classname= xd.CreateElement("class");//在<Root>之下建立元素<Tree>
classname.SetAttribute("name",b);
for(int k=0;k<dt2.Rows.Count;k++)
{
DataRow dr2=dt2.Rows[k];
if(dr2["mtvclassid"].ToString()==mtvclassid && dr2["type"].ToString()==type1[p])
{
XmlElement mtv= xd.CreateElement("mtv");//在<Root>之下建立元素<Tree>
// mtv.SetAttribute("NO","1");
XmlElement id= xd.CreateElement("id");//在<Root>之下建立元素<Tree>
id.SetAttribute("id",dr2["id"].ToString());
mtv.AppendChild(id);
XmlElement name= xd.CreateElement("name");//在<Root>之下建立元素<Tree>
name.SetAttribute("name",dr2["name"].ToString());
mtv.AppendChild(name);
XmlElement actors= xd.CreateElement("actors");//在<Root>之下建立元素<Tree>
actors.SetAttribute("actors",dr2["actors"].ToString());
mtv.AppendChild(actors);
XmlElement director= xd.CreateElement("director");//在<Root>之下建立元素<Tree>
director.SetAttribute("director",dr2["director"].ToString());
mtv.AppendChild(director);
XmlElement introduction= xd.CreateElement("introduction");//在<Root>之下建立元素<Tree>
introduction.SetAttribute("introduction",dr2["introduction"].ToString());
mtv.AppendChild(introduction);
XmlElement copyright= xd.CreateElement("copyright");//在<Root>之下建立元素<Tree>
copyright.SetAttribute("copyright",dr2["copyright"].ToString());
mtv.AppendChild(copyright);
XmlElement photo2= xd.CreateElement("photo2");//在<Root>之下建立元素<Tree>
photo2.SetAttribute("photo2",dr2["photo2"].ToString());
mtv.AppendChild(photo2);
XmlElement megsize= xd.CreateElement("megsize");//在<Root>之下建立元素<Tree>
megsize.SetAttribute("megsize",dr2["megsize"].ToString());
mtv.AppendChild(megsize);
XmlElement filename= xd.CreateElement("filename");//在<Root>之下建立元素<Tree>
filename.SetAttribute("filename",dr2["filename"].ToString());
mtv.AppendChild(filename);
classname.AppendChild(mtv);
}
}
type.AppendChild(classname);
// }
}
kind.AppendChild(type);
}
//----------------------------第二個分類
root.AppendChild(kind);//完成子節點<Tree>
foreach(string Name in System.IO.Directory.GetFiles(Server.MapPath(".")+"\\xml","mtv_"+dragent["number"].ToString()+"*.*"))
{
System.IO.File.Delete(Name);
}
DateTime currentTime;
currentTime = System.DateTime.Now;
int year=currentTime.Year;
int monty=currentTime.Month;
int day=currentTime.Day;
int hour=currentTime.Hour;
int minute=currentTime.Minute;
string str="";
str=year.ToString()+monty.ToString()+day.ToString()+hour.ToString()+minute.ToString();
string thetime="xml\\"+"mtv"+"_"+dragent["number"].ToString()+"_"+str+".xml";
xd.Save(Server.MapPath(thetime));
}