「XElement」オブジェクトを使用してCシャープ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();
}
}
}