Link to home
Start Free TrialLog in
Avatar of cimscims
cimscims

asked on

Append Date and Time to the XML file name in C#

I'm trying to create an XML file using the following statements.

XmlWriterSettings settings = new XmlWriterSettings();
using(XmlWriter xmlwriter = XmlWriter.Create(@"D:\Misc\BookRequest.xml",settings)

Instead of "BookRequest.XML", i would like to create the file name as "BookRequest_09192013 02:15:00 PM.xml".

Basically i would like to append the Current Date and Time to the name of the XML file.

Thanks in advance.
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

String pathname = "D:\Misc\BookRequest" + datetime.now.ToString("yyyy-MM-dd HH:mm tt") + ".xml"

XmlWriterSettings settings = new XmlWriterSettings();
using(XmlWriter xmlwriter = XmlWriter.Create(pathname,settings)
Avatar of cimscims
cimscims

ASKER

Path is not valid. Pls check the attachment.

Even i tried with pathname = @"D:\Misc\Book..." then the path is set as "D:\\Misc\\Book.."
It's putting extra "\" !!
Error.jpg
Hi cimscims;

The string pathname posted by @esskayb2d has some errors. The compile is interpreting the \M and \B in the pathname as control characters. To correcting the issue placing @ before the first double quote as shown in the statement below.

String pathname = @"D:\Misc\BookRequest" + datetime.now.ToString("yyyy-MM-dd HH:mm tt") + ".xml";
Hi FernandoSoto,

I tried using "@" before the statement. As i said earlier it's puttin extra "\" like D:\\Misc\\BookRequest...
Looking at strings in the Watch, or Locals Windows of the debugger or hovering over a string veritable will always escape the character \ by placing another \ in front of the first one. In other words if you have the following line of code:

 String path = @"D:\Misc\BookRequest";

Will be seen in all the three methods of viewing it as I stated above as the following:

"D:\\Misc\\BookRequest"

Having \\ in place of \. In a C# string the character following \ has a special meaning for example \n is a new line character \t is a tab character. In order for the compiler to know that \ is a forward slash with no special meaning you need to escape the \ by placing another \ next to it. Therefore that is not an issue and should not be causing a problem.

The following two lines in a C# code are the same.

String path = "D:\\Misc\\BookRequest";
String path = @"D:\Misc\BookRequest";
Extra "\" is not an issue for my problem.

it works fine when i do
String pathname = @"D:\Misc\BookRequest.xml";

It doesn't work when i try to append the Date and Time to the filename
String pathname = @"D:\Misc\BookRequest" + datetime.now.ToString("yyyy-MM-dd HH:mm tt") + ".xml";
Hi FernandoSoto,

Sorry we both posted our comments at the same time.

You are correct that the below statements are the same. That's what i tried to say in my earlier post.

String path = "D:\\Misc\\BookRequest";
String path = @"D:\Misc\BookRequest";
Yes it would cause a problem the way you posted it. C# code is case-sensitive datetime and now needs to be as shown in the code snippet below.

String pathname = @"D:\Misc\BookRequest" + DateTime.Now.ToString("yyyy-MM-dd HH:mm tt") + ".xml";

Open in new window

Still getting the error. Please see attached.
Error.jpg
This works
String pathname = @"D:\Misc\BookRequest" + DateTime.Now.ToString("yyyy-MM-dd") + ".xml";

If the time is appended then it says given path's format is not supported..
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks FernandoSoto..that worked.
Not a problem cimscims, glad to help.