2010-03-26 91 views
6

当我序列化一个对象,其中有一个DateTime它在XML字符串中返回空。XMlSerialization没有序列化日期时间

请参阅下文,了解我的XSD,从XSD生成的可序列化类以及处理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> 

串行:

 /// <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类

 //------------------------------------------------------------------------------ 
    // <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; 
       } 
      } 
     } 

为什么DateTime值被序列化为空字符串?

回答

12

您是否将DateOfBirthFieldSpecified设置为true?它将默认为false,意思是:不要序列化这个。

+0

是的,你是绝对正确的感谢alot.i已经看了很久,并不能找出发生了什么。再次感谢非常感谢! – Somedeveloper 2010-03-26 13:36:37

+0

因此,如果还有一个名为MyThingSpecified的属性,并且MyThingSpecified设置为true,则序列化程序将不会序列化MyThing? – 2013-02-05 17:03:21

+0

@Peter如果有一个MyThingSpecified,它将只序列化MyThing如果MyThingSpecified是** true ** – 2013-02-05 18:07:25