-1

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 :

enter image description here

Very strange... but it's ok now. Many thanks to all of you

Portekoi
  • 1,087
  • 2
  • 22
  • 44

3 Answers3

2

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.

Jon B
  • 51,025
  • 31
  • 133
  • 161
1

You try this:

Console.WriteLine(DateTime.UtcNow.ToString("s") + "Z");
R.You
  • 565
  • 3
  • 15
  • This is ok but if I try to put it in 'prop', I have the error "Unable to convert a string to DateTime" – Portekoi May 03 '16 at 15:52
0

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"));
Mostafiz
  • 7,243
  • 3
  • 28
  • 42