Reputation: 31
I have an xml file in following format :
<?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>
<appSettings>
<property name="EXTERNAL_FILESERVER" value="/site/bugbase.adobe.com/files/" />
<property name="FTP_USER" value="password" />
<property name="FTP_SERVER" value="sjshare.corp.adobe.com"/>
<property name="FTP_PASSWORD" value="password" />
<property name="FTP_READ_USER" value="password" />
<property name="FTP_READ_PASS" value="password" />
<property name="WORKFLOW_NOTIFICATION_TEMPLATE" value="util/workflow_notification_template.html"/>
</appSettings>
</server>
I wanted to change password values for "FTP_USER" and "FTP_READ_USER".
Code i tried so far :
XmlDocument doc = new XmlDocument();
string path = @"C:\Users\karansha\Desktop\config file 1.xml";
doc.Load(path);
doc.SelectNodes("/appSettings/property").Item(1).Attributes["value"].Value = "newpassword";
doc.SelectNodes("/appSettings/property").Item(2).Attributes["value"].Value = "new_password";
Upvotes: 1
Views: 171
Reputation: 1549
Here is the code to change the passwords
XDocument xDoc = XDocument.Load("C:\\test.xml");
if (xDoc != null)
{
IEnumerable<XElement> xEle = xDoc.XPathSelectElements("//property");
if (xEle != null)
{
foreach (XElement xE in xEle)
{
if (xE.Attribute("name") != null)
{
if(xE.Attribute("value") !=null)
{
if(string.compare(xE.Attribute("name").Value,"FTP_USER",true) ==0)
xE.Attribute("value").Value = "your new password"
if(string.compare(xE.Attribute("name").Value,"FTP_READ_USER",true) ==0)
xE.Attribute("value").Value = "your new password"
}
}
}
xDoc.Save("C:\\test.xml");
}
}
Upvotes: 2
Reputation: 11597
instead of doing
doc.SelectNodes("/appSettings/property").Item(1).Attributes["value"].Value = "password1";
use a foreach
loop
XmlDocument doc = new XmlDocument();
string path = @"C:\Users\karansha\Desktop\config file 1.xml";
doc.Load(path);
foreach (XmlNode selectNode in doc.SelectNodes("/appSettings/property"))
{
if(selectNode.Attributes["name"].Value.equals("FTP_USER") ||
selectNode.Attributes["name"].Value.equals("FTP_READ_USER"))
{
selectNode.Attributes["value"].Value = "new_password";
}
}
doc.Save(path);
and if need two different passwords
if(selectNode.Attributes["name"].Value.equals("FTP_USER"))
{
selectNode.Attributes["value"].Value = "new_password";
}
if(selectNode.Attributes["name"].Value.equals("FTP_READ_USER"))
{
selectNode.Attributes["value"].Value = "newPassword";
}
Upvotes: 1
Reputation: 769
Other options is put this data in xml file and use the XML deserialize to read the data, which is very simple.
XML File:
<?xml version="1.0"?>
<MyAppSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<EXTERNAL_FILESERVER>/site/bugbase.adobe.com/files/</EXTERNAL_FILESERVER>
<FTP_USER>password</FTP_USER>
<FTP_SERVER>sjshare.corp.adobe.com</FTP_SERVER>
<FTP_PASSWORD>password</FTP_PASSWORD>
<FTP_READ_USER>password</FTP_READ_USER>
<FTP_READ_PASS>password</FTP_READ_PASS>
<WORKFLOW_NOTIFICATION_TEMPLATE>util/workflow_notification_template.html</WORKFLOW_NOTIFICATION_TEMPLATE>
</MyAppSettings>
AppSettings Class:
public class MyAppSettings
{
public string EXTERNAL_FILESERVER { get; set; }
public string FTP_USER { get; set; }
public string FTP_SERVER { get; set; }
public string FTP_PASSWORD { get; set; }
public string FTP_READ_USER { get; set; }
public string FTP_READ_PASS { get; set; }
public string WORKFLOW_NOTIFICATION_TEMPLATE { get; set; }
}
Read data from XML.
private static void ReadConfig()
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyAppSettings));
FileStream fileStream = new FileStream("AppConfig.xml", FileMode.Open);
MyAppSettings ConfigData = (MyAppSettings)xmlSerializer.Deserialize(fileStream);
if (fileStream != null)
{
fileStream.Close();
}
}
Upvotes: 1
Reputation: 1549
Here is your answer
XDocument xDoc = XDocument.Load("C:\\test.xml");
if (xDoc != null)
{
IEnumerable<XElement> xEle = xDoc.XPathSelectElements("//property");
if (xEle != null)
{
int iPass = 0;
foreach (XElement xE in xEle)
{
if (xE.Attribute("value") != null)
{
xE.Attribute("value").Value = "password" + iPass;
iPass++;
}
}
xDoc.Save("C:\\test.xml");
}
}
Upvotes: 1