Write data to an xml file
-
Hi, How can i create and write to an xml file with C# the date and time in seperate tags to an xml file like this: ddMMyyyy HH:mm:ss Thanks in advance!
Hello In System.Xml namespace there are two classes:
XmlReader
andXmlWriter
. Use them to make the xml file and write the data to it.Regards:rose:
-
Hi, How can i create and write to an xml file with C# the date and time in seperate tags to an xml file like this: ddMMyyyy HH:mm:ss Thanks in advance!
Use the ToString method of the DateTime class to create the string representation of the date and the time. As you have used the exact format that the ToString method uses, it's easy: daDate.ToString("ddMMyyyy") and daDate.ToString("HH:mm:ss")
--- b { font-weight: normal; }
-
Use the ToString method of the DateTime class to create the string representation of the date and the time. As you have used the exact format that the ToString method uses, it's easy: daDate.ToString("ddMMyyyy") and daDate.ToString("HH:mm:ss")
--- b { font-weight: normal; }
Using daDate.ToString("HH:mm:ss") is a risky choice as ":" is a placeholder for the time separator. Hence for example Italian regional settings would give something like "21.17.48" instead of "21:17:48". Even in a fixed region users could simply break the format by changing their regional settings. Always provide a IFormatProvider, typically CultureInfo.InvarientCulture. If you create your own CultureInfo instance to do the formatting, remember specifying "false" to disallow custom overrides. I assume this is a fixed XML format that MUST be followed? If it is I guess you do not have a choice, but if it is in ANY way possible to influence the format do so, as it is basically as wrong as it can be. When storing dates as strings there are two formats you can use: "yyyy-MM-dd" or wrong. "ddMMyyyy" clearly belongs in the "wrong" category as it is ambigious and it doesn't even sort correctly. When storing date and time, preferably write them into a single entry with the ISO format. If you use the XmlSerializer, just serialize/deserialize a DateTime object and it will use the correct ISO format.
-
Using daDate.ToString("HH:mm:ss") is a risky choice as ":" is a placeholder for the time separator. Hence for example Italian regional settings would give something like "21.17.48" instead of "21:17:48". Even in a fixed region users could simply break the format by changing their regional settings. Always provide a IFormatProvider, typically CultureInfo.InvarientCulture. If you create your own CultureInfo instance to do the formatting, remember specifying "false" to disallow custom overrides. I assume this is a fixed XML format that MUST be followed? If it is I guess you do not have a choice, but if it is in ANY way possible to influence the format do so, as it is basically as wrong as it can be. When storing dates as strings there are two formats you can use: "yyyy-MM-dd" or wrong. "ddMMyyyy" clearly belongs in the "wrong" category as it is ambigious and it doesn't even sort correctly. When storing date and time, preferably write them into a single entry with the ISO format. If you use the XmlSerializer, just serialize/deserialize a DateTime object and it will use the correct ISO format.
lmoelleb wrote:
Using daDate.ToString("HH:mm:ss") is a risky choice as ":" is a placeholder for the time separator.
Yes, you are right. :) I didn't think of that. The character should be quoted to be interpreted as a literal:
"HH':'mm':'ss"
As the format now contains no culture variant components, it's not affected by any cultural differences. Using a format provider doesn't affect the output in this case, it's only encouraged for programming style. If no format provider is specified, the CultureInfo.CurrentCulture is used, as there exists no formatting without a format provider.--- b { font-weight: normal; }
-
Hi, How can i create and write to an xml file with C# the date and time in seperate tags to an xml file like this: ddMMyyyy HH:mm:ss Thanks in advance!
I assume that you already know how to write an XML output. So to segregate the Dat and time, you can use the DateTime class to get the date and time seperate portions from the DateTime.Now Just take out the time and date component from the supported methods
Excelsior Arjun Bahree "By The Might of Mjolnir" I Came! I Coded! I Conquered!