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