将SOAP消息解析为c#类

时间:2017-11-02 05:51:31

标签: c# soap

我正在尝试将SOAP消息解析为c#类,但是我没有收到任何错误,但SOAP消息中的数据仍未映射到类。

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

}

将SOAP解析为类的代码:

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

代码不会抛出任何错误,但DataSaving类的属性仍为空。

1 个答案:

答案 0 :(得分:1)

在属性

中将名称空间设为空
[XmlRoot(ElementName = "dataSaving", Namespace = "")]
public class DataSaving
{
    [XmlElement(ElementName = "prdoductType")]