我知道在C sharp xml文件中可以使用“XElement”对象加载,但万一用户必须加载多个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();
}
}
}
答案 0 :(得分:0)
我认为这就是你要找的东西:
var files = System.IO.Directory.GetFiles("C:\\Temp\\", "*.xml");
foreach (string file in files)
{
var element = XElement.Load(file);
Console.Write(element);
}
使用带有whild-cards的System.IO.Directory
类来查找所需的所有文件,然后在循环中处理它们。您还可以为多个目录创建外部循环。