XMlSerialization 未序列化日期时间

发布于 2024-08-26 15:42:35 字数 3970 浏览 6 评论 0原文

当我序列化一个包含 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;
             }
         }

Serialized 类

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2024-09-02 15:42:35

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文