tags:

views:

124

answers:

3

Hi ,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?

+10  A: 

You put the array reference into the string itself instead of concatenating it.

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

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.

Tim
You can achieve asyn processing without resorting to lots of additional threads. Using XmlReader/Writer you can asynchronously handle all of the files without creating a single new thread yourself. Yes you have to coordinate them but that's worth the effort.
No Refunds No Returns
I believe by "multiple files at once" the OP is referring to processing multiple files in contrast with processing a single file, but not about concurrency. (I'm surmising from the original source code.)
JMD
A: 

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
}
Kevin Brock
Thank you very much for your helps the program now works
aslı
I don't understand the down vote...for the answer that was accepted and worked. Can someone explain that please?
Kevin Brock
@Kevin Brock - The answer was a simple misplaced quotation mark. Your soultion only works becuase it rewrote the problematic line.
MiffTheFox
Thank you for letting me know your reason. Actually there were more problems than a misplaced quote. Look at `count` and the array. If there were fewer *.xml files in the directory, only some files from array would be processed; if there were more then there would be an out of bounds exception. Sorry, I wasn't trying to replace your simple answer.
Kevin Brock