.NET Xml序列化AnonymousType

时间:2011-12-07 13:35:04

标签: c# xml serialization anonymous

我正在开发一个序列化http xml响应的C#库。我的痕迹就像

Request1:

    <?xml version="1.0"?>
    <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
      <SOAP-ENV:Body>
        <axl xmlns="cisco_cme_xml_namespace">
          <request>
            <ISgetGlobal />
          </request>
        </axl>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
Response1:
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <SOAP-ENV:Body>
    <axl xmlns="cisco_cme_xml_namespace" >
    <response >
    <ISAddress>10.1.2.##</ISAddress>
    <ISMode>ITS</ISMode>
    <ISVersion>8.6</ISVersion>
    <ISDeviceRegistered>2</ISDeviceRegistered>
    <ISPeakDeviceRegistered>3</ISPeakDeviceRegistered>
    </response>
    </axl>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

Request2
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <axl xmlns="cisco_cme_xml_namespace">
      <request>
        <ISexecCLI>
          <CLI>ephone 4 exempt</CLI>
        </ISexecCLI>
      </request>
    </axl>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response2:
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <SOAP-ENV:Body>
    <axl xmlns="cisco_cme_xml_namespace" >
    <response >
    <ISexecCLIResponse>1</ISexecCLIResponse>
    <ISexecCLIError>Invalid CLI in XML message: ephone 4 exempt</ISexecCLIError>
    </response>
    </axl>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

我尝试将每个对象反序列化为单独的类。例如,我想反序列化对ISGlobalClass的第一个响应和第二个对ISExecCLIClass的响应。如何在反序列化期间决定响应类型? {我正在使用System.XML.XMLSerializer}

感谢,

编辑(克里斯):

嗨Chris,我的序列化类是Envelope。 {} Envelope.Body.Axl.Response Response的字段类型为Object。我试着在反序列化期间决定这个字段的正确类型。

 public partial class Response
    {

        private object itemField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("ISDevices", typeof(ISDevices))]
        [System.Xml.Serialization.XmlElementAttribute("ISError", typeof(ISError))]
        [System.Xml.Serialization.XmlElementAttribute("ISExtensions", typeof(ISExtensions))]
        [System.Xml.Serialization.XmlElementAttribute("ISGlobal", typeof(ISGlobal))]
        [System.Xml.Serialization.XmlElementAttribute("ISPresenceGlobal", typeof(ISPresenceGlobal))]
        [System.Xml.Serialization.XmlElementAttribute("ISSaveConfigResult", typeof(string))]
        [System.Xml.Serialization.XmlElementAttribute("ISSessionServers", typeof(ISSessionServers))]
        [System.Xml.Serialization.XmlElementAttribute("ISSipDevices", typeof(ISSipDevices))]
        [System.Xml.Serialization.XmlElementAttribute("ISSipExtensions", typeof(ISSipExtensions))]
        [System.Xml.Serialization.XmlElementAttribute("ISSipGlobal", typeof(ISSipGlobal))]
        [System.Xml.Serialization.XmlElementAttribute("ISUserProfiles", typeof(ISUserProfiles))]
        [System.Xml.Serialization.XmlElementAttribute("ISUtilityDirectory", typeof(ISUtilityDirectory))]
        [System.Xml.Serialization.XmlElementAttribute("ISVoiceHuntGroups", typeof(ISVoiceHuntGroups))]
        [System.Xml.Serialization.XmlElementAttribute("ISexecCLIResult", typeof(ISexecCLIResult))]
        [System.Xml.Serialization.XmlElementAttribute("ISuser", typeof(ISuser))]
        public object Item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }
    } 

0 个答案:

没有答案