1

我有一个以下格式的 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";
4

4 回答 4

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");
        }
    }
于 2013-08-09T03:57:22.510 回答
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>

应用设置类:

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();
            }
        }
于 2013-08-07T12:29:17.453 回答
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");
            }
        }
于 2013-08-07T12:09:50.160 回答
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";
    }
于 2013-08-07T11:53:07.023 回答