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);
            }


        }

猜你喜欢

转载自www.cnblogs.com/wdd812674802/p/10399334.html