Reputation: 27556
I'm using XDocument to create an XML file, as follows:
var d = DateTime.Now;
var xDocument = new XDocument(new XElement("ThisIsADate", d));
However, the resulting XML represents the date d using the xs:datetime format (e.g. "2012-05-11T00:00:00"). That is, it includes time information.
However, my XML is meant to match my XML Schema, which defines the element as being of type "xs:date". Consequently, the file is rejected when validated against the schema, because of the extra time information.
How can I fix this? I know I could just format the date myself using ToString() with a format, but this can't be the "right" way to do it, surely. I can't be expected to know how to format a date as a valid XML date - that's the job of the XML-related parts of the framework.
Edit: please note that I do know how to format a date using ToString(), and I also know what format string would give me the right result. That's not the answer I'm looking for. I'm looking for a function/method/class that understands what an xs:date (etc.) is, and that supports those kinds of encodings.
To be clear, I'm not looking to "get it done", I'm looking to "do it right". And re-inventing the XML wheel is not "doing it right" in my book.
Upvotes: 12
Views: 6971
Reputation: 1163
I fixed this problem using the following class for DateElements
private class XDateElement : XElement
{
public XDateElement(XName name, DateTime Date) :
base(name, Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))
{ }
}
The advantage of using a class like this is, that you have the specific date conversion only in one place.
var d = DateTime.Now;
var xDocument = new XDocument(new XDateElement("ThisIsADate", d));
Upvotes: 3
Reputation: 106936
As it already has been pointed out LINQ to XML is unable to produce a DateTime
value using the the xs:date
format. A DateTime
round-trips in LINQ to XML using the xs:dateTime
format and .NET does not a have a date-only type so it is not a suprise that the designers of LINQ to XML decided to only use xs:dateTime
to not complicate the API.
The only option is to format the date as a string giving you full control of the format. To correctly use the xs:date
format you need to convert the DateTime
to a string using this code:
d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
Using ToShortDateString
and/or not specifying a CultureInfo
will not produce the desired result.
Upvotes: 8
Reputation: 5101
A DateTime
always has both date and time components. So the trick is tell that DateTime
to output only it's date component. When you reconstitute the date on the receiving end, the time will default to midnight.
var XDocument = new XDocument(new XElement("ThisIsADate", DateTime.Now.ToShortDateString()));
The default form is M/d/yyyy
, but it still may not be in the right format! I don't know what your schema expects.
So use ToString("yourFormatStringHere")
var XDocument = new XDocument(new XElement("ThisIsADate", DateTime.Now.ToString("M-dd-yyyy")));
If you want to get really fancy, that is, write robust code... Pull the format string from your schema document and reference that in the ToString()
method rather than having an explicit but redundant format string.
Upvotes: -1
Reputation: 38230
There seems to be some confusion XML or as such the XElement will be storing the Value as a string. What is stored is more a work of the Xml Schema Validator.
Hence when you have to represent Date
you will have to pass in the string version of the Date part you can use the ToShortDateString()
or custom format in ToString()
.
When you are passing the DateTime instance its just calling the ToString() which contains the Time component too.
Hope this helps you.
Upvotes: 0