C# Soap XML SerializationException while parsing to object

时间:2018-12-19 11:21:59

标签: c# wcf soap

When i try to parse soap xml to objects, i'm getting below exception not sure what i'm doing wrong.

Exception:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll

Additional information: Error in line 1 position 687. Element 'http://soap.sforce.com/2005/09/outbound:sObject' contains data from a type that maps to the name 'urn:sobject.enterprise.soap.sforce.com:Contact'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'Contact' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to the serializer.

 static void Main(string[] args)
    {

        string inputString = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?> <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\"> <SessionId xsi:nil=\"true\"/> <EnterpriseUrl>https://hultibs--FullDev.cs10.my.salesforce.com/services/Soap/c/44.0/00DJ0000003QX7f</EnterpriseUrl> <PartnerUrl>https://hultibs--FullDev.cs10.my.salesforce.com/services/Soap/u/44.0/00DJ0000003QX7f</PartnerUrl> <Notification> <Id>04lJ000000PoRS2IAN</Id> <sObject xsi:type=\"sf:Contact\" xmlns:sf=\"urn:sobject.enterprise.soap.sforce.com\"> <sf:Id>0033600001koT9qAAE</sf:Id> <sf:Email>tcampbell2018@maili.com</sf:Email> <sf:Student_ID__c>5192435</sf:Student_ID__c> </sObject> </Notification> </notifications> </soapenv:Body> </soapenv:Envelope>";


        FromXml(inputString);
        Console.ReadLine();
    }

 public static void FromXml(string Xml)
    {
        using (var reader = XmlReader.Create(new StringReader(Xml)))
        {
            Message m = Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11);
            var body = m.GetBody<Notifications>();
            Console.WriteLine(body);
        }

    }
[DataContract(Name = "sObject", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public class SObject
    {
        [DataMember(Name = "Id", Order = 1)]
        public string Id { get; set; }
        [DataMember(Name = "Email", Order = 2)]
        public string Email { get; set; }
        [DataMember(Name = "Student_ID__c", Order = 3)]
        public string Student_ID__c { get; set; }
        [DataMember(Name = "type", Order = 4)]
        public string Type { get; set; }
        [DataMember(Name = "sf", Order = 5)]
        public string Sf { get; set; }
    }

    [DataContract(Name = "Notification", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public class Notification
    {
        [DataMember(Name = "Id", Order = 1)]
        public string Id { get; set; }
        [DataMember(Name = "sObject", Order = 2)]
        public SObject SObject { get; set; }
    }

    [DataContract(Name = "notifications", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public class Notifications
    {

        [DataMember(Name = "ActionId", Order = 2)]
        public string ActionId { get; set; }
        [DataMember(Name = "EnterpriseUrl", Order = 3)]
        public string EnterpriseUrl { get; set; }
        [DataMember(Name = "PartnerUrl", Order = 4)]
        public string PartnerUrl { get; set; }
        [DataMember(Name = "Notification", Order = 5)]
        public Notification Notification { get; set; }
    }

2 个答案:

答案 0 :(得分:0)

在异常中描述的问题是肥皂消息中sObject的以下类型和名称空间声明

<sObject xsi:type=\"sf:Contact\" xmlns:sf=\"urn:sobject.enterprise.soap.sforce.com\">

因为在该名称空间(或其他任何名称)中没有定义Contact类。

如果您从肥皂消息中的sObject中删除类型和名称空间声明(并从其成员中删除sf:前缀),它应该可以正常工作。

或删除xsi:type=\"sf:Contact\并将DataContract更改为

[DataContract(Name = "sObject", Namespace = "urn:sobject.enterprise.soap.sforce.com")]

或者保留肥皂消息并进行更改

    [DataContract(Name = "sObject", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public class SObject

    [DataContract(Name = "Contact", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public class Contact

也在更改(在通知中)

        [DataMember(Name = "sObject", Order = 2)]
        public SObject SObject { get; set; }

        [DataMember(Name = "sObject", Order = 2)]
        public Contact SObject { get; set; }

答案 1 :(得分:0)

您只需在DataContract中声明一个命名空间“ http://soap.sforce.com/2005/09/outbound”,就可以使用Message.CreateMessage来序列化您的Notifications并将您的xml与序列化的消息进行比较。

下面是代码。

static void Main(string[] args)
    {
        Notifications notifications = new Notifications()
        {
            ActionId = "actionId",
            EnterpriseUrl = "enterpriceUri",
            PartnerUrl = "parentUri",
            Notification = new Notification
            {
                Id = "abc",
                SObject = new SObject
                {
                    Email = "email",
                    Id = "id",
                    Sf = "sf",
                    Student_ID__c = "a",
                    Type = "type"
                }
            }
        };
        Message me = Message.CreateMessage(MessageVersion.Soap11, "www.abc.com", notifications);  // create a message and serialize the notifications into the message
        WriteMessage(me, @"d:\message.xml");


    }
    static void WriteMessage(Message message, string fileName)
    {

        using (XmlWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))
        {
            message.WriteMessage(writer);// write the message into a file
        }
        Process.Start(fileName);// show the file
    }

和序列化的消息。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">www.abc.com</Action></s:Header><s:Body><notifications xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://soap.sforce.com/2005/09/outbound"><ActionId>actionId</ActionId><EnterpriseUrl>enterpriceUri</EnterpriseUrl><PartnerUrl>parentUri</PartnerUrl><Notification><Id>abc</Id><sObject><Id>id</Id><Email>email</Email><Student_ID__c>a</Student_ID__c><type>type</type><sf>sf</sf></sObject></Notification></notifications></s:Body></s:Envelope>