简体   繁体   中英

How to load multiple files(*.xml) via XmlDocument C#

I wanted to load multiple .xml files in C#. Currently, I am able to load only 1 .xml file. But not able to find out how will be able to load multiple files .

My code:

XmlDocument doc = new XmlDocument();
string path = @"path of *.xml file";  //
doc.Load(path);

Your code doesn't have to bee too big, you just need to store all the paths in some collection, then you have to apply the same operation to each XML file.

string newValue = "1234";
XmlDocument doc;
var paths = new[] { "config1.xml", "config2.xml" };
paths.ToList().ForEach(path =>
{
    doc = new XmlDocument();
    doc.Load(path);

    // process the document
    var nm = new XmlNamespaceManager(doc.NameTable);
    var a = doc.SelectSingleNode("//SomeKeyValue", nm);
    a.InnerText = newValue;

    // save the file
    doc.Save(path);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM