0

I am trying to parse SOAP message to c# classes, however I am not getting any errors but still data from SOAP message is not mapped to classes.

SOAP MESSAGE:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
  <soap:Body>
    <mes:dataSaving xmlns:mes="http://message.test.testing.fi/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <prdoductType>test product</prdoductType>
      <action>save</action>
      <priority>1</priority>
      <timestamp>27.09.2017 09:12:34274000000</timestamp>
      <message priority="1" messageTimestamp="27.09.2017 09:12:34274000000">
        <![CDATA[<TestData><test>HOSTPITAL_1</test></TestData>]]>
      </message>
    </mes:dataSaving>
  </soap:Body>

C# Classes:

namespace Xml2CSharp
{
    [XmlRoot(ElementName = "message")]
    public class Message
    {
        [XmlAttribute(AttributeName = "priority")]
        public string Priority { get; set; }
        [XmlAttribute(AttributeName = "messageTimestamp")]
        public string MessageTimestamp { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "dataSaving", Namespace = "http://message.test.testing.fi/")]
    public class DataSaving
    {
        [XmlElement(ElementName = "prdoductType")]
        public string PrdoductType { get; set; }
        [XmlElement(ElementName = "action")]
        public string Action { get; set; }
        [XmlElement(ElementName = "priority")]
        public string Priority { get; set; }
        [XmlElement(ElementName = "timestamp")]
        public string Timestamp { get; set; }
        [XmlElement(ElementName = "message")]
        public Message Message { get; set; }
        [XmlAttribute(AttributeName = "mes", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Mes { get; set; }
        [XmlAttribute(AttributeName = "encodingStyle", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string EncodingStyle { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "dataSaving", Namespace = "http://message.test.testing.fi/")]
        public DataSaving DataSaving { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soap { get; set; }
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
    }

}

Code to parse SOAP to classes:

    /// <summary>
    /// Converts a SOAP string to an object
    /// </summary>
    /// <typeparam name="T">Object type</typeparam>
    /// <param name="soapMsg">SOAP string</param>
    /// <returns>The object of the specified type</returns>
    public T SOAPToObject<T>(string soapMsg)
    {
        try
        {
            var rawXML = XDocument.Parse(soapMsg);
            using (var reader = rawXML.CreateReader(System.Xml.Linq.ReaderOptions.None))
            {
                var ser = new XmlSerializer(typeof(T));
                return (T)ser.Deserialize(reader);
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

Code does not throw any error, however properties of DataSaving class are still null.

Gaurav123
  • 5,059
  • 6
  • 51
  • 81

1 Answers1

2

Make the name space empty in the attribute

[XmlRoot(ElementName = "dataSaving", Namespace = "")]
public class DataSaving
{
    [XmlElement(ElementName = "prdoductType")]
Ansil F
  • 284
  • 1
  • 8
  • Thanks a lot :) .. What about CDATA ? how it will be mapped – Gaurav123 Nov 02 '17 at 08:38
  • you need to do some custom serialization for CDATA .Refer https://stackoverflow.com/questions/1379888/how-do-you-serialize-a-string-as-cdata-using-xmlserializer – Ansil F Nov 02 '17 at 08:45