一个程序的数据库登陆用的配置文件
<?xml version="1.0" encoding="GB2312" ?>
<DataBase>
  <DataBase1>
    <SID>192.168.1.22</SID>
    <USERNAME>test</USERNAME>
    <PWD>test</PWD>
  </DataBase1>
</DataBase>请问用程序怎样修改这三个字段呢?

解决方案 »

  1.   

    xml会吗?来处理 xml就好了
      

  2.   

    xm读写
    http://www.cnblogs.com/ding25901/archive/2008/06/25/1229745.html
      

  3.   

    空间 System.Xml
    类XmlTextReader,XmlTextWriter等
      

  4.   


    可不可以做做一个dataTable来处理呢?
      

  5.   

    看一份xml文件的操作实例源代码,很简单的;
      

  6.   

                XmlDocument doc = new XmlDocument();
                string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Setting.config";//获得配置文件的全路径
                doc.Load(strFileName);
                XmlNodeList nodes = doc.GetElementsByTagName("add");//找出名称为“add”的所有元素
      

  7.   

                    XmlAttribute att = nodes[i].Attributes["key"];//获得将当前元素的key属性
      

  8.   


    string ConfigFile = Path.Combine(Application.StartupPath, @"XMLFile1.xml");private bool GetWebServiceMessage()
            {
                bool bRet = false;
                bool bVer = false;
                if (!File.Exists(ConfigFile))
                {
                    MessageBox.Show("系统配置文件不存在!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return bRet;
                }
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();                xmlDoc.Load(ConfigFile);
                    XmlElement element = xmlDoc.DocumentElement["DataBase1"] as XmlElement;                foreach (XmlNode node in element.ChildNodes)
                    {
                        if (node is XmlElement)
                        {
                            XmlElement ele = (XmlElement)node;
                            if (ele.Name.ToUpper().Trim().Equals("SID"))
                            {
                                bVer = Convert.ToBoolean(ele.InnerText);
                            }
                            if (ele.Name.ToUpper().Trim().Equals("USERNAME"))
                            {
                                bVer = Convert.ToBoolean(ele.InnerText);
                            }
                            if (ele.Name.ToUpper().Trim().Equals("PWD"))
                            {
                                bVer = Convert.ToBoolean(ele.InnerText);
                            }
                        }
                    }
                    xmlDoc.Save(ConfigFile);
                    xmlDoc = null;
                    bRet = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    bRet = false;
                }    }            return bRet;
            }
    private bool WriteToXml(string sVer)
            {
                bool bRet = false;            try
                {
                    XmlDocument xmlDoc = new XmlDocument();                xmlDoc.Load(ConfigFile);
                    XmlElement element = xmlDoc.DocumentElement["DataBase1"] as XmlElement;                foreach (XmlNode node in element.ChildNodes)
                    {
                        if (node is XmlElement)
                        {
                            XmlElement ele = (XmlElement)node;
                            if (ele.Name.ToUpper().Trim().Equals("SID"))
                            {
                                ele.InnerText = "";//你的值
                            }
                            if (ele.Name.ToUpper().Trim().Equals("USERNAME"))
                            {
                                ele.InnerText = "";//你的值
                            }
                            if (ele.Name.ToUpper().Trim().Equals("PWD"))
                            {
                                ele.InnerText = "";//你的值
                            }
                        }
                    }
                    xmlDoc.Save(ConfigFile);
                    xmlDoc = null;
                    bRet = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    bRet = false;
                }
                return bRet;
            }
    自己看看吧
      

  9.   


     
    //在开始之前,先建立一个smallfools.xml文件,内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <smallfoolsRoot>
       <poems>
         <author>王维</author>
         <title>竹里馆</title>
         <content>独坐幽篁里,弹琴复长啸。深林人不知,明月来相照。</content>
       </poems>
       <poems>
         <author>孟浩然</author>
         <title>宿建德江</title>
         <content>移舟泊烟渚,日暮客愁新。野旷天低树,江清月近人</content>
       </poems>
       <poems>
         <author>李白</author>
         <title>杜陵绝句</title>
         <content>南登杜陵上,北望五陵间。秋水明落日,流光灭远山</content>
       </poems>
       <poems>
         <author>李白</author>
         <title>望庐山瀑布</title>
         <content>日照香炉生紫烟,遥看瀑布挂前川。飞流直下三千尺,疑是银河落九天。</content>
       </poems>
       <poems>
         <author>李商隐</author>
         <title>锦瑟</title>
         <content>锦瑟无端五十弦,一弦一柱思华年。庄生晓梦迷蝴蝶,望帝春心托杜鹃。沧海月明珠有泪,蓝田日暖玉生烟。此情可待成追忆,只是当时已惘然。</content>
       </poems>
    </smallfoolsRoot>     //下面的操作都在这个xml文件里进行。//操作一:读取整个XML文件,并在DataGrid里显示出来:
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("smallfools.xml"));
    if (ds.Tables.Count>0)
    {
    this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
    this.DataGrid1.DataBind();
    }//操作二:获得第一个节点的值XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("smallfools.xml"));
    XmlNode xmlNode = xmlDoc.DocumentElement.FirstChild;
    if (xmlNode!=null)
    {
    this.tbauthor.Text = xmlNode["author"].InnerText;
    this.tbtitle.Text = xmlNode["title"].InnerText;
    this.tbcontent.Text = xmlNode["content"].InnerText;
    ViewState["Count"] = 0;
    }//操作三:查看某一个节点的内容
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("smallfools.xml"));
    XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
    XmlNode xmlNode = xmlNodeList.Item(0);
    this.tbauthor.Text = xmlNode["author"].InnerText;
    this.tbtitle.Text = xmlNode["title"].InnerText;
    this.tbcontent.Text = xmlNode["content"].InnerText;//操作四:添加一个节点
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("smallfools.xml"));
    //创建一个新节点
    XmlElement newElement = xmlDoc.CreateElement("poems");
    //创建newElement下的节点
    XmlElement elauthor = xmlDoc.CreateElement("author");
    XmlElement eltitle = xmlDoc.CreateElement("title");
    XmlElement elcontent = xmlDoc.CreateElement("content");
    elauthor.InnerText = this.tbaddauthor.Text.Trim();
    eltitle.InnerText = this.tbaddtitle.Text.Trim();
    elcontent.InnerText = this.tbaddcontent.Text.Trim();
    //将newElement下的节点加到newElement上
    newElement.AppendChild(elauthor);
    newElement.AppendChild(eltitle);
    newElement.AppendChild(elcontent);
    //将newElement加入到xml文件中(加在最后一条记录上)
    xmlDoc.DocumentElement.AppendChild(newElement);
    //如果要插到某条记录之后也可以用(加在第一条记录之后)
    //xmlDoc.DocumentElement.InsertAfter(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));
    //如果要插到某条记录之前也可以用(加在第一条记录之前)
    //xmlDoc.DocumentElement.InsertBefore(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));
    //存盘
    xmlDoc.Save(Server.MapPath("smallfools.xml"));//操作五:删除某个节点
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("smallfools.xml"));
    XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(0);
    xmlNode.ParentNode.RemoveChild(xmlNode);
    xmlDoc.Save(Server.MapPath("smallfools.xml"));//操作六:编辑某个节点
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("smallfools.xml"));
    //获得节点列表
    XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(1);
    xmlNode["author"].InnerText = this.tbauthor.Text;
    xmlNode["title"].InnerText = this.tbtitle.Text;
    xmlNode["content"].InnerText = this.tbcontent.Text;
    xmlDoc.Save(Server.MapPath("smallfools.xml"));//操作七:查找记录
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("smallfools.xml"));
    XmlNodeList nodelist = xmlDoc.SelectNodes("smallfoolsRoot/poems[author='"+this.tbsearch.Text.Trim()+"']");//操作八:糊模查找记录
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("smallfools.xml"));
    XmlNodeList nodelist = xmlDoc.SelectNodes("smallfoolsRoot/poems[contains(author,'"+this.tbsearch.Text.Trim()+"')]");