简体   繁体   中英

xml files with c#

I want to make a project in which more than one xml files will be processed at once .In this project I tried to put xml files in array and then I used but always I get errors.My code is like that:

    string[] files = { "ilk.xml", "migr.xml", "caa.xml" };
     public Form1()
    {
        InitializeComponent();

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\markets\");
        count = dir.GetFiles("*.xml").Length;

        for (int d = 0; d < count; d++)
        {

            XmlDocument xmlDoc1 = new XmlDocument();

            xmlDoc1.Load(files[d]);

            xmldocument= new XmlDocument();

            xmldocument.Load(@"C:\\markets\files[d]");

//here I compare the values of xml files

                     }

the error is "Could not find file 'C:\\markets\\files[d]".All the xml files are in the markets directory.When I wrote the file name without using array there is no problem .Can you help me?

您可以将数组引用放入字符串本身,而不是将其串联。

xmldocument.Load(@"C:\markets\" + files[d]);

You could use async loading and multi-threading if your really want to load them "at once" (as in, parallel at the same time). Probably though you want to just have all the XML documents parsed and loaded at the same time based on your comment that you will be comparing them. How about using an array of XmlDocument objects to match the input array of file names? This is the simplest way to load them all. You could use a collection too if you need that to be dynamic.

public void CompareXMLInDir(string path)
{
    DirectoryInfo dir = new DirectoryInfo(path);
    CompareXML(dir.GetFiles("*.xml"));
}

public void CompareXML(FileInfo[] xmlFileNames)
{
    XmlDocument[] xmlDocuments = new XmlDocument[xmlFileNames.Length];
    for(int i = 0; i < xmlFileNames.Length; i++)
    {
        xmlDocuments[i] = new XmlDocument();
        xmlDocuments[i].Load(xmlFileNames[i].FullName);
    }

    //...do compare work here
}

This code also allows you get retrieve all the files with ".xml" extension in the directory. This seems like something you are trying to do with the count, but then you reference the staticly defined file name array. This would result in an error if the number of XML files in that directory exceeded the file names in your array.

If you wanted just to use a fixed set of names, then you could do this:

public void CompareMyXMLFiles()
{
    string[] files = { "ilk.xml", "migr.xml", "caa.xml" };
    CompareXML(@"c:\markets", files);
}

public void CompareXML(string basePath, string[] xmlFileNames)
{
    XmlDocument[] xmlDocuments = new XmlDocument[xmlFileNames.Length];
    for(int i = 0; i < xmlFileNames.Length; i++)
    {
        xmlDocuments[i] = new XmlDocument();
        xmlDocuments[i].Load(basePath + @"\" + xmlFileNames[i]);
    }

    //...do compare work here
}

You mention processing multiple files at once.

The above code will not achieve that, you will want to look into threading to do that; you will need to move the processing of files into a separate function, and then create an instance of ThreadStart and Thread to activate your job. You will then need some logic for the list of directories and files it reads from to call the function in a separate thread dependant on the number of concurrent processes you want running.

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