1

I have the following XSD definition:

public class Test
{
    private System.DateTime time;

    [System.Xml.Serialization.XmlElementAttribute(DataType = "time")]
    public System.DateTime Time
    {
        get
        {
            return this.time;
        }
        set
        {
            this.time = value;
        }
    }
}

I now have the following code:

        var p = new Test
        {                
            Time = DateTime.UtcNow
        };

        var xml = ToXml(p);

        Console.WriteLine(xml);

        Console.ReadLine();

And the following to serialize the xml:

    public static string ToXml(Test value)
    {
        var settings = new XmlWriterSettings();
        settings.Encoding = new UnicodeEncoding(false, false);
        settings.Indent = false;
        settings.OmitXmlDeclaration = true;

        using (var textWriter = new StringWriter())
        {
            using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
                new XmlSerializer(typeof(Test)).Serialize(xmlWriter, value);

            return textWriter.ToString();
        }
    }

The above code outputs the time like this:

<Time>15:24:43.2588160+00:00</Time>

I would like this in UTC format. e.g.

<Time>15:24:43.00Z</Time>

How can I achieve this?

I have no control over the XSD but I can change the Serializer method if required.

Very similar to this question but I can't edit the XSD:

Serializing DateTime to time without milliseconds and gmt

Sun
  • 4,458
  • 14
  • 66
  • 108
  • It's got a specified offset of "+00:00" which is UTC already. I'd expect anything that understands XML datetime values properly to be fine with it. – Jon Skeet Mar 08 '18 at 15:36
  • No it gets rejected as the consuming software needs the format to be 15:24:43.00Z. But I have no control over this software!! ARGHH! – Sun Mar 08 '18 at 15:43
  • I would strongly urge you to *at least* report this as a bug in the consuming software. You may have no immediate control over it, but it may get fixed over time... I'd refer them to https://www.w3.org/TR/xmlschema-2/#dateTime – Jon Skeet Mar 08 '18 at 16:08
  • 1
    OK, but if the DataType attribute is removed then a UTC date doesn't have the +00:00 on the end. So why would a date be OK and a time not? – Sun Mar 08 '18 at 16:11
  • I apologise - I'd misread things the type that was involved. It looks like this really is a bug in .NET, given "Specifically, either the time zone must be omitted or, if present, the time zone must be Coordinated Universal Time (UTC) indicated by a "Z". " So the consuming software *is* right to reject it. My mistake. Hmm. – Jon Skeet Mar 08 '18 at 16:13
  • If you used my Noda Time package, you'd be able to use a `LocalTime` which I believe would be correctly serialized (without a time zone offset at all, which should be fine) but my guess is that that's not an option? – Jon Skeet Mar 08 '18 at 16:15
  • Hi Jon, I've looked into it but sorry to say it's not an option. Sorry. – Sun Mar 08 '18 at 18:27

1 Answers1

0

One way is to [XmlIgnore] your DateTime property and have a separate string property that will output/input the time in your specified format.

[XmlIgnore]
public System.DateTime Time
{
    get { return this.time; }
    set { this.time = value; }
}

[System.Xml.Serialization.XmlElementAttribute(DataType = "time")]
public string TimeString 
{
    get { return Time.ToString("yyyy-MM-dd"); }
    set { Time = DateTime.Parse(value); }
}
KingChezzy
  • 136
  • 7