Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string str = TextBox1.Text.ToString();
XmlDocument xdResp = new XmlDocument();
xdResp.LoadXml(str);


string strDate = DateTime.Now.ToShortDateString();
string strTime = DateTime.Now.ToLongTimeString();

string strSave = "XML_" + strDate + "_" + strTime ;
//Path.cha

string ext = "xml" ;

strSave = "XML_" + strDate + "_" + strTime + "." + ext;





My question is what should i write in xdResp.Save(); so that the file will be saved in the format " XML_CurrentDate_CurrentTime.xml"
Posted

You cannot use ToShortDateString() & ToLongTimeString() to compose the path of your file ; these will insert characters as '/' and ':' in your path, which are prohibited.

Better construct the path yourself, using string.Format() method ; for example :

C#
DateTime myDate = DateTime.Now;
string strDate = string.Format("{0}-{1}-{2}", myDate.Year, myDate.Month, myDate.Day);
string strTime = string.Format("{0}-{1}-{2}", myDate.Hour, myDate.Minute, myDate.Second);


Of course, you're free to use any separator you want, but it must not be a forbidden character for the filesystem you're trying to save to.

Hope this helps. Kind regards.
 
Share this answer
 
v2
Comments
PP29 22-Aug-11 9:37am    
Thanks Man :)
phil.o 22-Aug-11 10:24am    
You're welcome :)
Aman4.net 23-Aug-11 5:58am    
Everyone miss this point except you.
My Points 5+
phil.o 23-Aug-11 6:40am    
Thank's :)
Dear If matter is to save xdResp with StrSave FileName then use following:
xdResp.Save(strSave);


Please put code in code block and describe the complete problem always.

Regards,
Aman
 
Share this answer
 
v4
I would advice you to use String.Format because this gives you more control and is also easier on the eyes.
http://idunno.org/archive/2004/07/14/122.aspx[^]

Also check out these date time formatting examples (also pages with examples for other types):
http://www.csharp-examples.net/string-format-datetime/[^]

Good luck!
 
Share this answer
 
Comments
PP29 22-Aug-11 8:05am    
But the problem is that i am not able to attach extension like .xml in this case . strSav is containing the value Xml_CurrentDate_CurrentTime.xml but when i m providing the same to xdResp.Save(); it is throwing exception . Not a recognized path .
xdResp.Save(string.format("XML_{0}_{1}.Xml",DateTime.Today,DateTime.Now.ToLongTimeString()));
 
Share this answer
 
Comments
PP29 22-Aug-11 8:38am    
strSave = string.Format("XML_{0}_{1}.Xml", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString());
xdResp.Save(@"C:\Documents and Settings\Prashant_Prakash\My Documents\File\"+strSave);
But it is giving exception as :

The given path's format is not supported.
strSave = string.Format("XML_{0}_{1}.Xml", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString());
xdResp.Save(@"C:\Documents and Settings\Prashant_Prakash\My Documents\File\"+strSave);


I am giving this ...but it is giving exception as
The given path's format is not supported.
 
Share this answer
 
Comments
Oludayo Alli 22-Aug-11 8:52am    
Are you answering your question by yourself? Now you've accepted your own solution and you question has been marked SOLVED. Don't be surprise if people don't respond to this question again until you "Unaccept" this solution. But if you're satisfied with your own solution...I wish you all the best.

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