将SOAP xml反序列化为对象时出错

时间:2014-10-16 20:10:15

标签: c# xml web-services soap

我无法将传入的SOAP xml反序列化为生成的类对象。我收到了错误

" System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.InvalidOperationException:XML文档(1,2)中存在错误。 ---> System.InvalidOperationException:< Body xmlns =' http://schemas.xmlsoap.org/soap/envelope/'>没想到。在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read9_ArrayOfNotifications()"

XML我正在尝试反序列化

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <notifications xmlns="http://soap.sforce.com/2005/09/outbound">
   <OrganizationId>00DJ0000003K7uaMAC</OrganizationId>
   <ActionId>04kJ00000008Q0hIAE</ActionId>
   <SessionId>00DJ0000003K7ua!AR0AQAx0zs6_ygb_th_XWsRIh_RWuW29JcFnlUFOGvutkXmCRlvTzKEgFIOWEfcA93iOFSmnYts7TBY7qil7QzEbGnN4KeY0</SessionId>
   <EnterpriseUrl>https://cs10.salesforce.com/services/Soap/c/30.0/00DJ0000003K7ua</EnterpriseUrl>
   <PartnerUrl>https://cs10.salesforce.com/services/Soap/u/30.0/00DJ0000003K7ua</PartnerUrl>
   <Notification>
    <Id>04lJ00000097hkbIAA</Id>
    <sObject xsi:type="sf:Account" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
     <sf:Id>001J000001JMlMTIA1</sf:Id>
     <sf:Account_Code__c>AC-00001</sf:Account_Code__c>
     <sf:CreatedDate>2014-04-17T14:14:56.000Z</sf:CreatedDate>
     <sf:Name>test account12</sf:Name>
    </sObject>
   </Notification>
  </notifications>
 </soapenv:Body>
</soapenv:Envelope>

这里是wsdl生成的课程

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18444
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// 
// This source code was auto-generated by wsdl, Version=4.0.30319.17929.
// 
namespace GenomindSFDCOutboundMessages.Account {
    using System;
    using System.Web.Services;
    using System.Diagnostics;
    using System.Web.Services.Protocols;
    using System.Xml.Serialization;
    using System.ComponentModel;


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
    [System.Web.Services.WebServiceBindingAttribute(Name="NotificationBinding", Namespace="http://soap.sforce.com/2005/09/outbound")]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(sObject))]
    public interface INotificationBinding {

        /// <remarks/>
        [System.Web.Services.WebMethodAttribute()]
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
        [return: System.Xml.Serialization.XmlElementAttribute("notificationsResponse", Namespace="http://soap.sforce.com/2005/09/outbound")]
        notificationsResponse notifications([System.Xml.Serialization.XmlElementAttribute("notifications", Namespace="http://soap.sforce.com/2005/09/outbound")] notifications notifications1);
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://soap.sforce.com/2005/09/outbound")]
    public partial class notifications {

        private string organizationIdField;

        private string actionIdField;

        private string sessionIdField;

        private string enterpriseUrlField;

        private string partnerUrlField;

        private AccountNotification[] notificationField;

        /// <remarks/>
        public string OrganizationId {
            get {
                return this.organizationIdField;
            }
            set {
                this.organizationIdField = value;
            }
        }

        /// <remarks/>
        public string ActionId {
            get {
                return this.actionIdField;
            }
            set {
                this.actionIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string SessionId {
            get {
                return this.sessionIdField;
            }
            set {
                this.sessionIdField = value;
            }
        }

        /// <remarks/>
        public string EnterpriseUrl {
            get {
                return this.enterpriseUrlField;
            }
            set {
                this.enterpriseUrlField = value;
            }
        }

        /// <remarks/>
        public string PartnerUrl {
            get {
                return this.partnerUrlField;
            }
            set {
                this.partnerUrlField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Notification")]
        public AccountNotification[] Notification {
            get {
                return this.notificationField;
            }
            set {
                this.notificationField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://soap.sforce.com/2005/09/outbound")]
    public partial class AccountNotification {

        private string idField;

        private Account sObjectField;

        /// <remarks/>
        public string Id {
            get {
                return this.idField;
            }
            set {
                this.idField = value;
            }
        }

        /// <remarks/>
        public Account sObject {
            get {
                return this.sObjectField;
            }
            set {
                this.sObjectField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sobject.enterprise.soap.sforce.com")]
    public partial class Account : sObject {

        private string account_Code__cField;

        private System.Nullable<System.DateTime> createdDateField;

        private bool createdDateFieldSpecified;

        private System.Nullable<bool> is_Record_Deleted__cField;

        private bool is_Record_Deleted__cFieldSpecified;

        private string nameField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string Account_Code__c {
            get {
                return this.account_Code__cField;
            }
            set {
                this.account_Code__cField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public System.Nullable<System.DateTime> CreatedDate {
            get {
                return this.createdDateField;
            }
            set {
                this.createdDateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool CreatedDateSpecified {
            get {
                return this.createdDateFieldSpecified;
            }
            set {
                this.createdDateFieldSpecified = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public System.Nullable<bool> Is_Record_Deleted__c {
            get {
                return this.is_Record_Deleted__cField;
            }
            set {
                this.is_Record_Deleted__cField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool Is_Record_Deleted__cSpecified {
            get {
                return this.is_Record_Deleted__cFieldSpecified;
            }
            set {
                this.is_Record_Deleted__cFieldSpecified = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string Name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Account))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(AggregateResult))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sobject.enterprise.soap.sforce.com")]
    public partial class sObject {

        private string[] fieldsToNullField;

        private string idField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("fieldsToNull", IsNullable=true)]
        public string[] fieldsToNull {
            get {
                return this.fieldsToNullField;
            }
            set {
                this.fieldsToNullField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string Id {
            get {
                return this.idField;
            }
            set {
                this.idField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sobject.enterprise.soap.sforce.com")]
    public partial class AggregateResult : sObject {

        private System.Xml.XmlElement[] anyField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement[] Any {
            get {
                return this.anyField;
            }
            set {
                this.anyField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://soap.sforce.com/2005/09/outbound")]
    public partial class notificationsResponse {

        private bool ackField;

        /// <remarks/>
        public bool Ack {
            get {
                return this.ackField;
            }
            set {
                this.ackField = value;
            }
        }
    }
}

这是我收到邮件时正在执行的方法。我使用SOAP UI发送消息进行测试

[WebMethod]
public void EchoSoapRequest(int input)
{
    // Initialize soap request XML
    XmlDocument xmlSoapRequest = new XmlDocument();          

    // Get raw request body
    using (Stream receiveStream = HttpContext.Current.Request.InputStream)
    {
        // Move to begining of input stream and read
        receiveStream.Position = 0;
        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
        {
            // Load into XML document
            xmlSoapRequest.Load(readStream);
        }
    }

    var notifications = xmlSoapRequest.GetElementsByTagName("soapenv:Body")[0].OuterXml;

    using (XmlReader reader = XmlReader.Create(new StringReader(notifications)))
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(OutboundMessages.Account.notifications));
        object obj = (OutboundMessages.Account.notifications)deserializer.Deserialize(reader);
    }            

}

我不确定为什么我无法反序列化xml内容。

0 个答案:

没有答案