网站的配置文件XML读写
网站的配置信息一般都写入到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);