Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMlSerialization is not serializing a Datetime

When I serialize an object which has a DateTime in it this is returning empty in the XML string.

Please see below for my XSD, serializable class generated from the XSD, and serialization helper class which handles the serialization of the XSD.

XSD:

 <?xml version="1.0" encoding="utf-8"?>
     <xs:schema id="test" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="testInformation">
       <xs:complexType>
        <xs:sequence>
             <xs:element name="DateOfBirth" minOccurs="0">
               <xs:simpleType>
                 <xs:restriction base="xs:date">
                   <xs:pattern value="\d{4}-\d{2}-\d{2}" />
                 </xs:restriction>
               </xs:simpleType>
             </xs:element>
           </xs:sequence>
         </xs:complexType>
      </xs:element>
     </xs:schema>

Serializer:

     /// <summary>
         /// This static class provides methods which can be used to help with
 common xml serialiazation tasks.
         /// </summary>
         public static class XmlSerializationHelper
         {
                     public static string
 SerializeObject<ObjectToSerialize>(ObjectToSerialize
 obj)
             {
                 string responseXML = string.Empty;
                 using (MemoryStream ms = new MemoryStream())
                 using (StreamWriter output = new StreamWriter(ms,
 Encoding.UTF8))
                 using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
                 {
                     XmlSerializer xmlSerializer = new
 XmlSerializer(typeof(ObjectToSerialize));
                     xmlSerializer.Serialize(output, obj);
                     ms.Position = 0;

                     responseXML = sr.ReadToEnd();
                 }
                 return responseXML;
             }
         }

Serializable class

     //------------------------------------------------------------------------------
     // <auto-generated>
     //     This code was generated by a tool.
     //     Runtime Version:2.0.50727.3607
     //
     //     Changes to this file may cause incorrect behavior and will be
 lost if
     //     the code is regenerated.
     // </auto-generated>
     //------------------------------------------------------------------------------

     // 
     // This source code was auto-generated by xsd,
 Version=2.0.50727.42.
     // 

         using System.Xml.Serialization;


         /// <remarks/>
         [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd",
 "2.0.50727.42")]
         [System.SerializableAttribute()]
         [System.Diagnostics.DebuggerStepThroughAttribute()]
         [System.ComponentModel.DesignerCategoryAttribute("code")]
         [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
         [System.Xml.Serialization.XmlRootAttribute(Namespace="",
 IsNullable=false)]
         public partial class testInformation {

             private System.DateTime dateOfBirthField;

             private bool dateOfBirthFieldSpecified;

             /// <remarks/>
             [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
             public System.DateTime DateOfBirth {
                 get {
                     return this.dateOfBirthField;
                 }
                 set {
                     this.dateOfBirthField = value;
                 }
             }

             /// <remarks/>
             [System.Xml.Serialization.XmlIgnoreAttribute()]
             public bool DateOfBirthSpecified {
                 get {
                     return this.dateOfBirthFieldSpecified;
                 }
                 set {
                     this.dateOfBirthFieldSpecified =
 value;
                 }
             }
         }

Why is the DateTime value being serialized into an empty string?

like image 337
Somedeveloper Avatar asked Mar 26 '10 13:03

Somedeveloper


People also ask

Can you serialize DateTime?

For serializing, you can use the DateTime(Offset). ToString method in your converter write logic. This allows you to write DateTime and DateTimeOffset values using any of the standard date and time formats, and the custom date and time formats.

What is the limitation of XML serialization?

XML serialization has some limitations. It converts public property values and fields, but does not encode type information. Also it does not encode private properties and fields, which requires binary serialization. Any class to be serialized to XML must have a public, parameterless constructor.

What is serializing XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.


1 Answers

Are you setting DateOfBirthFieldSpecified to true ? It will default to false, meaning: don't serialize this.

like image 80
Marc Gravell Avatar answered Oct 12 '22 04:10

Marc Gravell