1

I am trying to deserialise some XML that we are receiving into objects, however the PubdateMemberString property is always 1/1/0001. I added the member property after it failed to automatically convert the date. Below is a sample C# console program that does just this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace xml_deserialisation_test_c
{
    class Program
    {
        
        public static void Main()
        {
            XElement parameters = XElement.Parse("<Items><SundryItem><BookingKey>X Ref Charge</BookingKey><Dealprice>7500</Dealprice><Quantity>1</Quantity><PubDate>2022-03-26</PubDate></SundryItem><SundryItem><BookingKey>Featured job</BookingKey><Dealprice>39600</Dealprice><Quantity>1</Quantity><PubDate>2022-03-26</PubDate></SundryItem><SundryItem><BookingKey>Email Link</BookingKey><Dealprice>0</Dealprice><Quantity>1</Quantity><PubDate>2022-03-26</PubDate></SundryItem></Items>" );
            var items = GetSundryItems(parameters);
        }

        private static List<SundryItem>GetSundryItems(XElement source)
        {
            var x = new System.Runtime.Serialization.DataContractSerializer(typeof(SundryItem));
            return (from ele in source.Elements()
                    select (SundryItem)x.ReadObject(ele.CreateReader())).ToList();
        }

        [DataContract(Namespace = "", Name = "SundryItem")]
        [Serializable()]
        [System.Xml.Serialization.XmlRoot(ElementName = "SundryItem")]
        public partial class SundryItem
        {
            [DataMember]
            public string BookingKey { get; set; }
            [DataMember]
            public decimal Dealprice { get; set; }
            [DataMember]
            public int Quantity { get; set; }
            [DataMember(Name = "PubDate")]
            public string PubdateMemberString { get; set; }
            [IgnoreDataMember]
            public DateTime PubDate { get; set; }

            [OnSerializing]
            private void OnSerialise(StreamingContext c)
            {
                PubdateMemberString = PubDate.ToString("yyyy-MM-dd");
            }

            [OnDeserialized]
            private void OnDeserialised(StreamingContext c)
            {
                // PubDate = Date.Parse(PubdateMemberString)
            }
        }
    }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • DataContractSerializer is order-sensitive, and the `` elements are out of order. See [Does Order Matter in the XML Read by the DataContractSerializer?](https://stackoverflow.com/q/2250111) (answer: yes) and [WCF Disable Deserialization Order Sensitivity](https://stackoverflow.com/q/1727682) (answer: can't do it without reverting to `XmlSerializer`.) See https://dotnetfiddle.net/CmUGsV (no order) vs https://dotnetfiddle.net/MyPNB6 (with `Order=X` attributes applied). In fact this looks to be a duplicate of those two, agree? – dbc Mar 21 '22 at 18:21
  • I've solved this problem many times before. You date in the xml file does not match the local culture. So Use following. Change format as required : DateTime _PubDate { get; set; } public string PubDate { get {return _PubDate.ToString("ddmmyy" ; } set { _PubDate = DateTime.ParseExact(value, "ddmmyy", System.Globalization.CultureInfo.InvariantCulture; } } – jdweng Mar 22 '22 at 09:08
  • @dbc It is a duplicate of the first link. However, that link didn't come up. so If you add it as an answer I'll mark it as correct. – Phoenix Stoneham Mar 22 '22 at 09:14

0 Answers0