Conversion of XML file to MessageContract class not picking up certain elements RRS feed

  • Question

  • I am trying to consume an XML file via MessageContract for testing a WCF service. I looked around in quite a few places and have kind of exhausted my other options.

    Based on this question: How can I convert an XML file to an instance of a MessageContract class?;

    So far am able to read the XML file, but it only gets the top level elements. Members that make up their own class (DataContract) are not being picked up. SenderID, TransactionID and Company are populated fine, but Person and Carrier are nulls/0. Any help is greatly appreciated. Thanks.

    Below is the MessageContract and the code used to read the XML.

    var bodyReader = XmlReader.Create("File.xml");
    var msg = Message.CreateMessage(bodyReader, int.MaxValue, MessageVersion.Soap11);
    var converter = TypedMessageConverter.Create(typeof(Example), 
                    null, "http://test/rat1", new XmlSerializerFormatAttribute());
    var request = (Example)converter.FromMessage(msg);


    public class Example
        public string SenderID;
        public string TransactionId { get; set; }
        public string Company { get; set; }
        [MessageBodyMember(Namespace = "http://test/domain/rat2")]
        public PersonMessage Person { get; set; }
        [MessageBodyMember(Namespace = "http://test/domain/rat3")]
        public CarrierMessage[] Carrier
            get { return (_carrier == null) ? null : _carrier.ToArray(); }
            set { _carrier = (value == null) ? null : value.ToList(); }

    Person class

    public class PersonMessage
        [DataMember(IsRequired = true)]
        public PersonName Name { get; set; }
        [DataMember(IsRequired = false)]
        public string Identifier { get; set; }
        [DataMember(IsRequired = false)]
        public short Score { get; set; }
        [DataMember(IsRequired = false)]
        public ScoreTypeEnum? ScoreType { get; set; }
    Wednesday, December 3, 2014 9:09 PM

All replies

  • You will need to deserialize the XML into an instance of your data contract.  This is what WCF is doing for you under the covers.

    Here is a quick tutorial that will show you how to invoke the DataContractSerializer manually for your XML.

    Thursday, December 4, 2014 6:30 AM
  • Thanks for the reply. Tried that and am getting this error: 

    Error in line 1 position 387. Expecting element 'Example' from namespace ''.. Encountered 'Element'  with name 'Envelope', namespace ''.

    The example does not have headers while my xml file does. I tried removing the header and keeping only the body needed for deserialization, but still similar error. 

    Thursday, December 4, 2014 2:14 PM