-1

I need to change password values in my configuration.xml .

Password need to be changed for following users :

  1. tsuer1
  2. github
  3. wtsntro
  4. wtsntrw and so on.

Format of configuration.xml file is like following :

<?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>

And following is the code i tried but failed :

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();
6
  • What did it fail with? Commented Aug 8, 2013 at 11:31
  • Right, but i think there is some problem with the doc.SelectNodes ? Commented Aug 8, 2013 at 11:33
  • @Romoku : It doesn't give any error message but still fails to perform changes in configuration.xml file. Commented Aug 8, 2013 at 11:35
  • @AseemKamaal Debug it and find out why. Commented Aug 8, 2013 at 11:35
  • @aevitas: I debugged it and find that my code fails to go into foreach loop. Seems some problem in doc.SelectNodes Commented Aug 8, 2013 at 11:39

2 Answers 2

2

Your Xml elements are contained in a NameSpace, so your XPath needs to account for that.

See:

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");
Sign up to request clarification or add additional context in comments.

2 Comments

One more thing this is the case when user needs to do it in single file, what to do in case of multiple files.
If the namespace and schema are the same then it'll work. If the namespace is different then @alexfilipocici's solution works.
0

You have to specify the namespace:

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))
{
    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.