码迷,mamicode.com
首页 > Web开发 > 详细

网站的配置文件XML读写

时间:2015-07-13 18:02:28      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考。

读操作:
        XmlDocument xmlDoc = new XmlDocument();
             XmlReaderSettings settings = new XmlReaderSettings();
             settings.IgnoreComments = true;//忽略文档里面的注释
             var path = HttpContext.Server.MapPath("../../SiteConfig/Siteconfig.xml");//获取文件路径
             var myXDocument = XDocument.Load(path);
             EditWebConfigViewModel siteconfigModel = new EditWebConfigViewModel();
             siteconfigModel.WebName = myXDocument.Element("SiteConfig").Element("WebName").Value;
             siteconfigModel.CompanyName = myXDocument.Element("SiteConfig").Element("CompanyName").Value;
             siteconfigModel.Address = myXDocument.Element("SiteConfig").Element("Address").Value;
             siteconfigModel.Tel = myXDocument.Element("SiteConfig").Element("Tel").Value;
             siteconfigModel.Email = myXDocument.Element("SiteConfig").Element("Email").Value;
             siteconfigModel.RecordNumber = myXDocument.Element("SiteConfig").Element("RecordNumber").Value;
             siteconfigModel.SeoTitle = myXDocument.Element("SiteConfig").Element("SeoTitle").Value;
             siteconfigModel.SeoKey = myXDocument.Element("SiteConfig").Element("SeoKey").Value;
             siteconfigModel.SeoDescription = myXDocument.Element("SiteConfig").Element("SeoDescription").Value;
             siteconfigModel.Copyright = myXDocument.Element("SiteConfig").Element("Copyright").Value;
写操作:
         var path = HttpContext.Server.MapPath("../../SiteConfig/Siteconfig.xml");
                var myXDocument = XDocument.Load(path);
                myXDocument.Element("SiteConfig").Element("WebName").Value = editWebConfig.WebName;
                myXDocument.Element("SiteConfig").Element("CompanyName").Value = editWebConfig.CompanyName;
                myXDocument.Element("SiteConfig").Element("Address").Value = editWebConfig.Address;
                myXDocument.Element("SiteConfig").Element("Tel").Value = editWebConfig.Tel;
                myXDocument.Element("SiteConfig").Element("Email").Value = editWebConfig.Email;
                myXDocument.Element("SiteConfig").Element("RecordNumber").Value = editWebConfig.RecordNumber;
                myXDocument.Element("SiteConfig").Element("SeoTitle").Value = editWebConfig.SeoTitle;
                myXDocument.Element("SiteConfig").Element("SeoKey").Value = editWebConfig.SeoKey;
                myXDocument.Element("SiteConfig").Element("SeoDescription").Value = editWebConfig.SeoDescription;
                myXDocument.Element("SiteConfig").Element("Copyright").Value = editWebConfig.Copyright;
                myXDocument.Save(path);

 

网站的配置文件XML读写

标签:

原文地址:http://www.cnblogs.com/WZH75171992/p/4643267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!