我可以通过XElement加载多个xml文件吗

本文关键字:xml 文件 可以通过 XElement 加载 | 更新日期: 2023-09-27 18:27:03

我知道在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();
        }
    }
}

我可以通过XElement加载多个xml文件吗

我想这就是您想要的:

var files = System.IO.Directory.GetFiles("C:''Temp''", "*.xml");
foreach (string file in files)
{
    var element = XElement.Load(file);
    Console.Write(element);
}

使用带有whild卡的System.IO.Directory类来查找所需的所有文件,然后在循环中处理它们。您还可以对多个目录进行外循环。