<Datas>
  <Connection>
    <database>start</database>
    <basetype>Oracle</basetype>
    <username>scott</username>
    <password>tiger</password>
  </Connection>
  <Connection>
    <database>goodluck</database>
    <basetype>Oracle</basetype>
    <username>scott</username>
    <password>tiger</password>
  </Connection>
  <Connection>
    <database>hello</database>
    <basetype>Oracle</basetype>
    <username>scott</username>
    <password>tiger</password>
  </Connection>
  <Connection>
    <database>conn</database>
    <basetype>Oracle</basetype>
    <username>scott</username>
    <password>tiger</password>
  </Connection>
<Datas>
以上是XML文件,我现在想选定一个记录,然后修改Username和Password,请问如何修改,请附上代码

解决方案 »

  1.   

    刚才自己解决了,呵呵
                      XElement xel = XElement.Load("C:\\DataConfig.xml");
                    var item = from x in xel.Descendants("Connection")
                               where (string)x.Element("database") == textService.Text
                               select x;
                    foreach (var c in item)
                    {
                        c.Element("username").ReplaceWith(new XElement("username", textUsername.Text));
                        c.Element("password").ReplaceWith(new XElement("password", textPassword.Text));
                    }
                    xel.Save("C:\\DataConfig.xml");
                    MessageBox.Show("数据源信息修改成功!");
      

  2.   

    恩,最近刚开始学,觉得这个linq挺好用的