2010-03-26 41 views
6

Khi tôi tuần tự hóa một đối tượng có một số DateTime, nó sẽ trả về trống trong chuỗi XML.XMlSerialization không tuần tự hóa một datetime

Vui lòng xem bên dưới cho XSD của tôi, lớp có thể tuần tự được tạo từ XSD và lớp trình trợ giúp tuần tự xử lý tuần tự hóa 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 lớp

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

Tại sao là DateTime giá trị đang được tuần tự hóa thành một chuỗi rỗng?

Trả lời

12

Bạn đang đặt DateOfBirthFieldSpecified thành true? Nó sẽ mặc định là false, có nghĩa là: không serialize này.

+0

Vâng, bạn hoàn toàn cảm ơn rất nhiều. Tôi đã xem xét điều này cho các lứa tuổi và không thể tìm ra những gì đang diễn ra. cảm ơn một lần nữa nhiều đánh giá cao! – Somedeveloper

+0

Vậy ... serializer sẽ không serialize MyThing nếu có thuộc tính được gọi là MyThingSpecified ... và MyThingSpecified được đặt thành true? –

+0

@Peter nếu có một MyThingSpecified, nó sẽ chỉ serialize MyThing nếu MyThingSpecified là ** true ** –

Các vấn đề liên quan