XML : Deserializing xml with different declaration

I need to deserialize following XML into an object inC#

the XML:

  <rdlt version="1.5" xmlns:rdlt="http://www.rdlt.org" xmlns="http://www.rdlt.org">      <created>2016-02-18T15:36:29.326+00:00</created>      <updated>2016-02-18T15:36:29.326+00:00</updated>  </rdlt>    

The object:

  [XmlType(AnonymousType = true)]  [XmlRoot(Namespace = "http://www.rdml.org")]  public class Container  {      [XmlElement(Namespace = "http://www.rdml.org")]      public DateTime created { get; set; }      [XmlElement(Namespace = "http://www.rdml.org")]      public DateTime updated { get; set; }  }    

The deserialization:

  var reader = XmlReader.Create(@"C:\tmp\test.xml");  var serializer = new DataContractSerializer(typeof(Container));  var object = serializer.ReadObject(reader);    

I get the following error that the elements and schema's do not match:

   Additional information: Error in line 1 position 2. Expecting element    'Container' from namespace 'http://schemas.datacontract.org/2004/07/ConsoleApplication1'..   Encountered 'Element'  with name 'rdlt', namespace 'http://www.rdlt.org'.    

Allready tried

  • Adding the rdlt name to the root
  • reader.MoveToContent();
  • XmlReader

No comments:

Post a Comment