0

I am using the following code to loop through a directory, looking for xml files and readind them in:

XmlReader reader = null;

foreach (string file in files)
{
   try
   {
     System.IO.FileInfo fi = new System.IO.FileInfo(file);

     string fext = fi.Extension;
     if (fext == ".xml")
     {
         Console.WriteLine("Processing file:" + fi.Name);
         reader = XmlReader.Create(fi.Name);
       **//BUT THIS WORKS---> reader = new XmlReader(@"\\10.00.100.11   \Data\Cognos\ReportOutput\Test\Risk Rating Exception Detail (LN-133-D)-en-us_2012-04-14T031017814Z-pdf_desc.xml");**

          while (reader.Read())
          {
              switch (reader.NodeType)
              {
                 case XmlNodeType.Element: // The node is an element.
                      Console.Write("<" + reader.Name);
                      Console.WriteLine(">");
                      break;
                 case XmlNodeType.Text: //Display the text in each element.
                      Console.WriteLine(reader.Value);
                      break;
                 case XmlNodeType.EndElement: //Display the end of the element.
                      Console.Write("</" + reader.Name);
                      Console.WriteLine(">");
                      break;
              }

           }

           reader.Close();

       }

    }
    catch (System.IO.FileNotFoundException e)
    {
     // If file was deleted by a separate application or thread since the call to TraverseTree() then just continue.
         Console.WriteLine(e.Message);
         continue;
    }

}

When I use XML.Create on the single file (see BUT THIS WORKS), I can read everything in that document. When I use it with fi.Name, I get to see the message "Processing file: ", then, for every single xml file in the directory, I see "Could not find file 'C:\Documents and Settings\\My Documents\Visual Studio 2010\Projects\MoveReportsTest\MoveReportsTest\bin\Debug\.

I tried moving the reader instantiation around, at first, it was instantiated for every file, I tried moving the reader.Close() around, thinking I can't instantiate the same reader for every file, but it didn't change anything (same error). The 'could not find file message is not coming from any catch phrase... I am clueless... please help! Thanks!

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
MariusD
  • 129
  • 1
  • 2
  • 14
  • fi.Name seems suspicious. You may want fi.FullName instead. You can also try adding "catch(Exception exception)" to catch more than just FileNotFound. That could help troubleshoot. – Steve Wong Apr 17 '12 at 14:50

2 Answers2

1

As you refer to fi.Name it picks just the name of the file. If you in the path you provide only the name of the file, the default path will be the folder where you binaries are, so that one you see in exception : C:\Documents and Settings\\My Documents\Visual Studio 2010\Projects\MoveReportsTest\MoveReportsTest\bin\Debug\.

Pass complete path to the XML file you gonna read => fi.FullName.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • That was the problem, thank you, however, now it's behaving erratically, I get to see a bunch of tags before I even get to see the "Processing file:.." display, which makes no sense. Am I using the reader.Close() correctly? (just ouside the loop through the xml file)? Thanks a lot!! – MariusD Apr 17 '12 at 14:58
  • thanks again, it looks like is stops processing after reading in 2 files, I will look into it. – MariusD Apr 17 '12 at 15:12
0

By the error, it sounds like you are searching for a file in a relative location, however keep in mind that it will look for a relative location if you use fi.Name only, I would suggest using fi.FullName if you are not using a relative location.

FileInfo -> C:/Location/File.Ext
FileInfo.Name -> File.Ext
FileInfo.FullName -> C:/Location/File.Ext

reader = XmlReader.Create(fi.FullName);
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180