【问题标题】:XMlSerialization is not serializing a DatetimeXMlSerialization 没有序列化日期时间
【发布时间】:2011-02-01 04:25:29
【问题描述】:

当我序列化一个包含DateTime 的对象时,它在 XML 字符串中返回空。

关于我的 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;
             }
         }

可序列化类

     //------------------------------------------------------------------------------
     // <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 值被序列化为空字符串?

【问题讨论】:

    标签: c# datetime serialization xsd xmlserializer


    【解决方案1】:

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

    【讨论】:

    • 是的,你是绝对正确的,非常感谢。我已经看这个很久了,无法弄清楚发生了什么。再次感谢非常感谢!
    • 所以...如果还有一个名为 MyThingSpecified 的属性...并且 MyThingSpecified 设置为 true,则序列化程序不会序列化 MyThing?
    • @Peter 如果有 MyThingSpecified,它只会在 MyThingSpecified 为 true 时序列化 MyThing
    • 今天偶然发现这个问题,你的回答解决了,非常感谢。
    • 谢谢 .. 我发现它也适用于枚举,因为日期时间或枚举的对象不能设置为 null .. 所以应该有另一种方法来允许序列化,除了使用 null 和使用另一个 bool ***字段...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多