我知道在 C 中可以使用“XElement”对象加载清晰的 xml 文件,但万一用户必须加载多个 xml 文件。如果没有,那么可以做些什么来做到这一点。任何帮助都值得赞赏...
代码 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml.Linq;
using System.Xml;
namespace ConsoleApplication85
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
string path = @"C:\Users\karansha\Desktop\configuration.xml"; // location of configuration.xml file.
doc.Load(path);
var nm = new XmlNamespaceManager(doc.NameTable);
nm.AddNamespace("jb", "urn:jboss:domain:1.2");
// Using foreach loop for specific Xmlnodes.
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".
Console.WriteLine("tsuser1 passowrd changed");
}
if (selectNode.Attributes["name"].Value == "watson.git_pwd") //github
{
selectNode.Attributes["value"].Value = "new passwordx"; // changes password value for "FTP_READ_USER".
Console.WriteLine("github password changed");
}
if (selectNode.Attributes["name"].Value == "FTP_READ_PASS") // wtsntro
{
selectNode.Attributes["value"].Value = "new_passwordy"; // changes password value for "FTP_PASSWORD".
Console.WriteLine("wtsntro password changed");
}
}
doc.Save(path); // Save changes.
Console.WriteLine("Password changed successfully");
Console.ReadLine();
}
}
}