I need have this format : aaaa-mm-jjThh:mm:sszzzzzz
And put it in a XML property "DateTime" type.
So, I did it :
var xmlObj= new xmlObj.tHeader();
xmlObj.prop = DateTime.ParseExact(DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture);
Console.WriteLine(xmlObj.prop);
The Console.WriteLine instruction return : 03/05/2016 15:43:10
I don't understand why the property remove the format.
In the XSD, this property is waiting a datetime format.
Any ideas?
EDIT :
Ok, on the command result, I see a default format but If convert my XML object to File, the format is correct :
Very strange... but it's ok now. Many thanks to all of you
You are calling DateTime.ParseExact
, which parses a string into a DateTime
object. Just drop that part and assign the result of ToString
directly to xmlObj.prop
, or assign the DateTime
object directly (if that's what it's looking for).
Edit:
To address your edit, your XML file is generated correctly. However, when you output the DateTime prop to the console, it uses the default string format for a date. You can format that with ToString()
if you want to.
您可以尝试以下方法:
Console.WriteLine(DateTime.UtcNow.ToString("s") + "Z");
Your format is wrong in parse, if you want yyyy-MM-ddTHH:mm:ssz
format then you have to convert it as string otherwise, it will represent standard datetime, try this
xmlObj.prop = DateTime.ParseExact(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ"), "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
Console.WriteLine(xmlObj.prop.ToString("yyyy-MM-ddTHH:mm:ssz"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.