将SOAP XML反序列化为对象在C#中显示为null

时间:2019-07-13 10:38:46

标签: c# soap xml-deserialization

我编写了以下代码来反序列化SOAP XML:

using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace SoapToClassData
{
    class Program
    {
        static void Main(string[] args)
        {
            var soap = @"<SOAP-ENV:Envelope
                xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""
                xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/""
                xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
                xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
                xmlns:cwmp=""urn:dslforum-org:cwmp-1-0"">
                <SOAP-ENV:Header>
                    <cwmp:ID SOAP-ENV:mustUnderstand=""1"">279384</cwmp:ID>
                </SOAP-ENV:Header>
                <SOAP-ENV:Body>
                    <cwmp:Inform>
                        <DeviceId>
                            <Manufacturer>Huawei Technologies Co., Ltd</Manufacturer>
                            <OUI>00259E</OUI>
                            <ProductClass>EG8040H5</ProductClass>
                            <SerialNumber>48575443FF5E5D9D</SerialNumber>
                        </DeviceId>
                        <Event SOAP-ENC:arrayType=""cwmp:EventStruct[1]"">
                            <EventStruct>
                                <EventCode>2 PERIODIC</EventCode>
                                <CommandKey/>
                            </EventStruct>
                        </Event>
                        <MaxEnvelopes>1</MaxEnvelopes>
                        <CurrentTime>2019-06-19T15:30:33+00:00</CurrentTime>
                        <RetryCount>0</RetryCount>
                        <ParameterList SOAP-ENC:arrayType=""cwmp:ParameterValueStruct[8]"">
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.ManagementServer.ParameterKey</Name>
                                <Value xsi:type=""xsd:string""/>
                            </ParameterValueStruct>
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name>
                                <Value xsi:type=""xsd:string"">http://10.240.12.35:7547/1d9564b694ef18090a9377cd6f3217eb</Value>
                            </ParameterValueStruct>
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.DeviceSummary</Name>
                                <Value xsi:type=""xsd:string"">InternetGatewayDevice:1.4[](Baseline:1, EthernetLAN:1, WiFiLAN:2, Time:1, IPPing:1, DeviceAssociation:1)</Value>
                            </ParameterValueStruct>
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.DeviceInfo.SpecVersion</Name>
                                <Value xsi:type=""xsd:string"">1.0</Value>
                            </ParameterValueStruct>
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.DeviceInfo.HardwareVersion</Name>
                                <Value xsi:type=""xsd:string"">172D.A</Value>
                            </ParameterValueStruct>
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.DeviceInfo.SoftwareVersion</Name>
                                <Value xsi:type=""xsd:string"">V5R019C00S115</Value>
                            </ParameterValueStruct>
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.DeviceInfo.ProvisioningCode</Name>
                                <Value xsi:type=""xsd:string""/>
                            </ParameterValueStruct>
                            <ParameterValueStruct>
                                <Name>InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress</Name>
                                <Value xsi:type=""xsd:string"">10.240.12.35</Value>
                            </ParameterValueStruct>
                        </ParameterList>
                    </cwmp:Inform>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>";

            var rawXML = XDocument.Parse(soap);
            Envelope deserializedObject;
            using (var reader = rawXML.CreateReader(ReaderOptions.None))
            {
                var ser = new XmlSerializer(typeof(Envelope));
                deserializedObject = (Envelope)ser.Deserialize(reader);
            }

            Console.ReadKey();
        }
    }

    [XmlRoot(ElementName = "ID", Namespace = "urn:dslforum-org:cwmp-1-0")]
    public class ID
    {
        [XmlAttribute(AttributeName = "mustUnderstand", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string MustUnderstand { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Header
    {
        [XmlElement(ElementName = "ID", Namespace = "urn:dslforum-org:cwmp-1-0")]
        public ID ID { get; set; }
    }

    [XmlRoot(ElementName = "DeviceId")]
    public class DeviceId
    {
        [XmlElement(ElementName = "Manufacturer")]
        public string Manufacturer { get; set; }
        [XmlElement(ElementName = "OUI")]
        public string OUI { get; set; }
        [XmlElement(ElementName = "ProductClass")]
        public string ProductClass { get; set; }
        [XmlElement(ElementName = "SerialNumber")]
        public string SerialNumber { get; set; }
    }

    [XmlRoot(ElementName = "EventStruct")]
    public class EventStruct
    {
        [XmlElement(ElementName = "EventCode")]
        public string EventCode { get; set; }
        [XmlElement(ElementName = "CommandKey")]
        public string CommandKey { get; set; }
    }

    [XmlRoot(ElementName = "Event")]
    public class Event
    {
        [XmlElement(ElementName = "EventStruct")]
        public EventStruct EventStruct { get; set; }
        [XmlAttribute(AttributeName = "arrayType", Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
        public string ArrayType { get; set; }
    }

    [XmlRoot(ElementName = "Value")]
    public class Value
    {
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName = "ParameterValueStruct")]
    public class ParameterValueStruct
    {
        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }
        [XmlElement(ElementName = "Value")]
        public Value Value { get; set; }
    }

    [XmlRoot(ElementName = "ParameterList")]
    public class ParameterList
    {
        [XmlElement(ElementName = "ParameterValueStruct")]
        public List<ParameterValueStruct> ParameterValueStruct { get; set; }
        [XmlAttribute(AttributeName = "arrayType", Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
        public string ArrayType { get; set; }
    }

    [XmlRoot(ElementName = "Inform", Namespace = "urn:dslforum-org:cwmp-1-0")]
    public class Inform
    {
        [XmlElement(ElementName = "DeviceId")]
        public DeviceId DeviceId { get; set; }
        [XmlElement(ElementName = "Event")]
        public Event Event { get; set; }
        [XmlElement(ElementName = "MaxEnvelopes")]
        public string MaxEnvelopes { get; set; }
        [XmlElement(ElementName = "CurrentTime")]
        public string CurrentTime { get; set; }
        [XmlElement(ElementName = "RetryCount")]
        public string RetryCount { get; set; }
        [XmlElement(ElementName = "ParameterList")]
        public ParameterList ParameterList { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "Inform", Namespace = "urn:dslforum-org:cwmp-1-0")]
        public Inform Inform { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Header Header { get; set; }
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "SOAP-ENV", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string SOAPENV { get; set; }
        [XmlAttribute(AttributeName = "SOAP-ENC", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string SOAPENC { 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; }
        [XmlAttribute(AttributeName = "cwmp", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Cwmp { get; set; }
    }

}

但是我得到的所有数据都为空,如下所示:

enter image description here

有人可以告诉我,我在哪里弄错了吗?

0 个答案:

没有答案