Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C# to download 2 xml files:

C#
using (FileStream fs = new FileStream(Path.Combine(Properties.Settings.Default.XmlLocation, DateTime.Now.ToString("yyyyMMddHHmm") + " File2.xml"), FileMode.Create))
               {
                   "\n");
                   client2.DownloadFile(location);
                   Program.processLog.Write("XML File 2 Retrieved");
                   fs.Close();
               }


I then parse the XML file as follows:


C#
XmlSerializer deserializer = new XmlSerializer(typeof(DOGS), new XmlRootAttribute("DOGS"));
                   using (XmlReader reader = XmlReader.Create(Path.Combine(Properties.Settings.Default.XmlLocation, DateTime.Now.ToString("yyyyMMddHHmm") + " File2.xml")))
                   {

                       xmlReadFileOne = (DOGS)deserializer.Deserialize(reader);
                       Program.processLog.Write("File 1 Successfully Deserialized");
                       return xmlReadFileOne;  
                   }



The problem I am having is:

If I retrieve the XML file from the server at 15:26

If I then try and read the same XML file at 15:27

I receive an error that file does not exist because the times do not match.

I think its because when I am reading the file with XML reader I am using XMLReader.create, when all I want to do it read the file perhaps? I am not sure??

Also in the directory there are loads of XML files as I download a file every 5 mins, so I need to ensure I read the latest XML file when deserializing.

Please can you help me resolve this?
Posted

1 solution

The simple answer is: never retrieve the time twice if you need to use it again: it may well change!

So store it - or even store the filename - as I did in my answer to your previous question:
C#
string fileName = @"D:\MyFolder\" + DateTime.Now.ToString("yyyyMMddHHmmss") + " Dogs.xml";

You then know you are accessing the same file.
If you are doing this in different methods, and you can't pass it from one to the other, then find the latest version - it's just a change on the code I gave you before:
VB
string latest = Directory.GetFiles(@"D:\MyFolder", "* Dogs.xml")
                    .Select(s => new FileInfo(s))
                    .OrderByDescending(fi => fi.CreationTime)
                    .Select(fi => fi.FullName)
                    .FirstOrDefault();
 
Share this answer
 
Comments
xirokx 8-Jul-14 11:20am    
awesome....thanks alot
RaisKazi 8-Jul-14 12:33pm    
I would rather use GUId instead relying on DateTime/TimeStamp. :)
OriginalGriff 8-Jul-14 14:16pm    
Guid has advantages, but in this case I'd be against it: he is creating then relatively infrequently, so name conflict is extremely unlikely. Using a Date and Time allows users to look at them and view them in chronological order - which is harder if you use a Guid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900