我有以下格式的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>
<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>
我想更改“FTP_USER”和“FTP_READ_USER”的密码值。
我到目前为止尝试的代码:
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";
答案 0 :(得分:2)
以下是更改密码的代码
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");
}
}
答案 1 :(得分:1)
而不是
doc.SelectNodes("/appSettings/property").Item(1).Attributes["value"].Value = "password1";
使用foreach
循环
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);
如果需要两个不同的密码
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";
}
答案 2 :(得分:1)
这是你的答案
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");
}
}
答案 3 :(得分:1)
其他选项是将此数据放在xml文件中,并使用XML反序列化来读取数据,这非常简单。
XML文件:
<?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类:
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; }
}
从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();
}
}