加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – XMlSerialization没有序列化Datetime

发布时间:2020-12-15 17:40: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/XMLSch
当我序列化一个有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值序列化为空字符串?

解决方法

你是否将DateOfBirthFieldSpecified设置为true?它将默认为false,这意味着:不要序列化它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读