自定义一个config文件夹,包含不同的配置文件,如果实现读写,如discuz!nt一定,有没有现成的类和简单的一些方法

解决方案 »

  1.   


    自定义一个config文件夹,包含网站运行的多个的配置文件,如何实现读写,如discuz!nt一样,所有的配置文件都放在config文件夹下,并不是我们常用的web.confg文件, 有没有现成的类和简单的一些方法,直接实现看了一个NT的,好像有点复杂
    =============================
    今天的输入法不知是怎么了老出问题
    =============================
      

  2.   

    那就是操作xml文件了。
    可以把操作xml文件的方法封装了。
    建议使用xpath这个东西蛮方便的。
      

  3.   

    引用System.Xml命名空间,参考一下这段代码:        private void WriteConfigFile(string FilePath)
            {
                FilePath = string.Format("{0}\\{1}", CurrentPath.Substring(0, CurrentPath.LastIndexOf('\\')), FilePath);
                if (!File.Exists(FilePath))
                {
                    MessageBox.Show("Web配置文件丢失,请尝试重新安装解决该问题。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }            XmlDocument document = new XmlDocument();
                document.Load(FilePath);
                XmlNode appSettingNode = document.SelectSingleNode("/configuration/connectionStrings");
                XmlNode dbConnectionNode = document.SelectSingleNode("/configuration/connectionStrings/add[@name='" + ConnectionStringNodeName + "']");
                if (dbConnectionNode != null)
                {
                    dbConnectionNode.Attributes["connectionString"].Value = GetConnectionString();
                    dbConnectionNode.Attributes["providerName"].Value = ProviderName;
                }
                else
                {
                    XmlElement elementAdd = document.CreateElement("add");
                    XmlAttribute keyAttribute = document.CreateAttribute("name");
                    keyAttribute.Value = ConnectionStringNodeName;
                    XmlAttribute valueAttribute = document.CreateAttribute("connectionString");
                    valueAttribute.Value = this.m_connection.ConnectionString;
                    XmlAttribute providerAttribute = document.CreateAttribute("providerName");
                    providerAttribute.Value = ProviderName;
                    elementAdd.Attributes.Append(keyAttribute);
                    elementAdd.Attributes.Append(valueAttribute);
                    elementAdd.Attributes.Append(providerAttribute);
                    appSettingNode.AppendChild(elementAdd);
                }
                try
                {
                    document.Save(FilePath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
      

  4.   

    那应该就算是普通的读取XML文本文件的操作了吧。
      

  5.   

    给你个简单例子,注意所保存的对象必须支持序列化:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;namespace BNCore.Common.Config
    {
        public class ConfigurationXmlReadWriteHelper
        {
            public static void SaveConfig(string path, object mObject)
            {            StreamWriter XmlWriter = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("gb2312"));
                XmlWriter.Write(GetXML(mObject));
                XmlWriter.Close();
            }        private static string GetXML(object mObject)
            {
                XmlSerializer oSerializer = new XmlSerializer(mObject.GetType());
                StringWriter oStringWriter = new StringWriter();
                oSerializer.Serialize(oStringWriter, mObject);
                return oStringWriter.ToString();
            }
            private static object LoadXML(System.Type mClass, string XML)
            {
                XmlSerializer oSerializer = new XmlSerializer(mClass);
                StringReader oStringReader = new StringReader(XML);
                return oSerializer.Deserialize(oStringReader);
            }
            public static object LoadConfig(System.Type mClass, string path)
            {
                StreamReader XmlReader = new StreamReader(path, System.Text.Encoding.GetEncoding("gb2312"));
                object obj = LoadXML(mClass, XmlReader.ReadToEnd());
                XmlReader.Close();
                return obj;
            }
        }
    }