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.
cimscimsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ess KayEntrapenuerCommented:
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)
0
cimscimsAuthor Commented:
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
0
Fernando SotoRetiredCommented:
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";
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

cimscimsAuthor Commented:
Hi FernandoSoto,

I tried using "@" before the statement. As i said earlier it's puttin extra "\" like D:\\Misc\\BookRequest...
0
Fernando SotoRetiredCommented:
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";
0
cimscimsAuthor Commented:
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";
0
cimscimsAuthor Commented:
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";
0
Fernando SotoRetiredCommented:
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

0
cimscimsAuthor Commented:
Still getting the error. Please see attached.
Error.jpg
0
cimscimsAuthor Commented:
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..
0
Fernando SotoRetiredCommented:
Sorry about that the : can NOT be used in a file name so you will need to change that character to something else as in the code snippet below I have changed : to _  .

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cimscimsAuthor Commented:
Thanks FernandoSoto..that worked.
0
Fernando SotoRetiredCommented:
Not a problem cimscims, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.