您的位置:首页 > 编程语言 > ASP

asp.net 生成XML文件

2007-01-11 17:41 399 查看
本代码从数据库中读出记录,有多层的套用,来生成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));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐