更改XML文件中的值

时间:2013-08-08 11:27:11

标签: c#

我需要在配置文件中更改密码值。

以下用户需要更改密码:

  1. tsuer1
  2. github上
  3. wtsntro
  4. wtsntrw等等。
  5. configuration.xml文件的格式如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <server xmlns="urn:jboss:domain:1.2">
      <extensions>
        <extension module="org.jboss.as.clustering.infinispan" />
        <extension module="org.jboss.as.cmp" />
        <extension module="org.jboss.as.ejb3" />
      </extensions>
      <system-properties>
        <property name="SERVER" value="JBOSS" />
        <property name="FTP_USER" value="adobenet\wtsntrw" />
        <property name="FTP_PASSWORD" value="password value" />
        <property name="FTP_READ_USER" value="adobenet\\wtsntro" />
        <property name="FTP_READ_PASS" value="password value" />
        <property name="API_SECRET_KEY" value="wxrocks" />
        <property name="API_ENV" value="regular" />
        <property name="PRERELEASE_PASSWORD" value="prerelease" />
        <property name="watson.git_user" value="github" />
        <property name="watson.git_pwd" value="password value" />
        <property name="teststudio.user" value="tsuser1" />
        <property name="teststudio.pwd" value="password value" />
      </system-properties>
    </server>
    

    以下是我尝试但失败的代码:

    XmlDocument doc = new XmlDocument();
                string path = @"C:\Users\karansha\Desktop\configuration.xml";  // location of configuration.xml file.
                doc.Load(path);
                // Using foreach loop for specific Xmlnodes.
                foreach (XmlNode selectNode in doc.SelectNodes("server/system-properties/property"))
                {
                    if (selectNode.Attributes["name"].Value == "teststudio.pwd")  // tsuser1
                    {
                        selectNode.Attributes["value"].Value = "new password";  // changes password value for "FTP_USER".
                    }
    
                    if (selectNode.Attributes["name"].Value == "watson.git_pwd")   //github
                    {
                        selectNode.Attributes["value"].Value = "new passwordx";  // changes password value for "FTP_READ_USER".
                    }
                    if (selectNode.Attributes["name"].Value == "FTP_READ_PASS")   // wtsntro
                    {
                        selectNode.Attributes["value"].Value = "new_passwordy";  // changes password value for "FTP_PASSWORD".
                    }
                }
    
                doc.Save(path);  // Save changes.
                Console.WriteLine("Password changed successfully");
                Console.ReadLine();
    

2 个答案:

答案 0 :(得分:2)

您的Xml元素包含在NameSpace中,因此您的XPath需要考虑到这一点。

请参阅:

XPath on an XML document with namespace

XML Namespaces and How They Affect XPath and XSLT

XmlDocument doc = new XmlDocument();
doc.Load(path);

var nm = new XmlNamespaceManager(doc.NameTable);
nm.AddNamespace("jb", "urn:jboss:domain:1.2");

foreach (XmlNode selectNode in doc.SelectNodes("jb:server/jb:system-properties/jb:property", nm))
{
    if (selectNode.Attributes["name"].Value == "teststudio.pwd")  // tsuser1
    {
        selectNode.Attributes["value"].Value = "new password";  // changes password value for "FTP_USER".
    }

    if (selectNode.Attributes["name"].Value == "watson.git_pwd")   //github
    {
        selectNode.Attributes["value"].Value = "new passwordx";  // changes password value for "FTP_READ_USER".
    }

    if (selectNode.Attributes["name"].Value == "FTP_READ_PASS")   // wtsntro
    {
        selectNode.Attributes["value"].Value = "new_passwordy";  // changes password value for "FTP_PASSWORD".
    }
}

doc.Save(path);  // Save changes.
Console.WriteLine("Password changed successfully");

答案 1 :(得分:0)

您必须指定命名空间:

var nm = new XmlNamespaceManager(doc.NameTable);
if (doc.ChildNodes.Count != 2)
    throw new XmlException("Document is not well formated.");
var serverNode = doc.ChildNodes[1];
nm.AddNamespace("a", serverNode.NamespaceURI);
foreach (XmlNode selectNode in 
    doc.SelectNodes("a:server/a:system-properties/a:property", nm))
{
    // ...
}