c#創建xml文檔方法


 protected void MakeMXL(string newsid)
    {
        News news = NewsManager.GetNewsByNewsId(Convert.ToInt32(newsid));
        if (news.NewsId > 0)
        {
            //  創建XML文檔,存在就刪除再生成
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null);
            doc.AppendChild(dec);
            //  創建根結點
            XmlElement XMLroot = doc.CreateElement("root");
            doc.AppendChild(XMLroot);

            //創建信息節點
            XmlElement newsroot = doc.CreateElement("News");

            XmlElement xmlnewsid = doc.CreateElement("News_ID");
            xmlnewsid.InnerText = news.NewsId.ToString();
            newsroot.AppendChild(xmlnewsid);

            XmlElement xmlnewstitle = doc.CreateElement("News_Title");
            xmlnewstitle.InnerText = news.NewsTitle.ToString();
            newsroot.AppendChild(xmlnewstitle);

            XmlElement xmlnewstime = doc.CreateElement("News_Time");
            xmlnewstime.InnerText = news.NewsTime.ToShortDateString();
            newsroot.AppendChild(xmlnewstime);

            XmlElement xmlnewsAuthor = doc.CreateElement("News_Author");
            xmlnewsAuthor.InnerText = news.NewsAuthor.ToString();
            newsroot.AppendChild(xmlnewsAuthor);

            XmlElement xmlnewsContent = doc.CreateElement("News_Content");
            xmlnewsContent.InnerText = news.NewsContent.ToString();
            newsroot.AppendChild(xmlnewsContent);

            XMLroot.AppendChild(newsroot);

            string xmlFile = comFunction.GetRootPath() + "\\XML\\" + newsid + ".XML";
            //  Response.Write(xmlFile);
            try
            {
                doc.Save(xmlFile);
                Console.Write(doc.OuterXml);
                //  Message.Alert(LsUser.SelectedItem.Text + "用戶,權限生成成功!", Page);
            }
            catch
            {
                Message.Alert("生成失敗!", Page);
            }


        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM