asp.net 產生XML檔案

來源:互聯網
上載者:User

本代碼從資料庫中讀出記錄,有多層的套用,來產生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));
   }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.