C# Soap XML SerializationException 解析到对象时

当我尝试将soap xml解析为对象时,我遇到了异常,不确定我做错了什么。


例外:


System.Runtime.Serialization.dll 中出现“System.Runtime.Serialization.SerializationException”类型的未处理异常


附加信息:第 1 行位置 687 中的错误。元素“ http://soap.sforce.com/2005/09/outbound:sObject ”包含映射到名称“urn:sobject.enterprise.soap.sforce”的类型的数据.com:联系方式”。反序列化器不知道映射到此名称的任何类型。如果您正在使用 DataContractSerializer,请考虑使用 DataContractResolver,或者将与“Contact”对应的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到传递给序列化程序的已知类型列表中。


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

        }


    }

胡子哥哥
浏览 232回答 2
2回答

MMMHUHU

异常中描述的问题在于soap消息中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")]或者留下肥皂信息,然后改变&nbsp; &nbsp; [DataContract(Name = "sObject", Namespace = "http://soap.sforce.com/2005/09/outbound")]&nbsp; &nbsp; public class SObject至&nbsp; &nbsp; [DataContract(Name = "Contact", Namespace = "urn:sobject.enterprise.soap.sforce.com")]&nbsp; &nbsp; public class Contact也在变化(在通知中)&nbsp; &nbsp; &nbsp; &nbsp; [DataMember(Name = "sObject", Order = 2)]&nbsp; &nbsp; &nbsp; &nbsp; public SObject SObject { get; set; }至&nbsp; &nbsp; &nbsp; &nbsp; [DataMember(Name = "sObject", Order = 2)]&nbsp; &nbsp; &nbsp; &nbsp; public Contact SObject { get; set; }

蝴蝶不菲

您只需在 DataContract 中声明一个命名空间“ http://soap.sforce.com/2005/09/outbound ”,您可以使用 Message.CreateMessage 序列化您的通知并将您的 xml 与序列化消息进行比较。下面是代码。static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Notifications notifications = new Notifications()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActionId = "actionId",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnterpriseUrl = "enterpriceUri",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PartnerUrl = "parentUri",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Notification = new Notification&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Id = "abc",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SObject = new SObject&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Email = "email",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Id = "id",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sf = "sf",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student_ID__c = "a",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type = "type"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Message me = Message.CreateMessage(MessageVersion.Soap11, "www.abc.com", notifications);&nbsp; // create a message and serialize the notifications into the message&nbsp; &nbsp; &nbsp; &nbsp; WriteMessage(me, @"d:\message.xml");&nbsp; &nbsp; }&nbsp; &nbsp; static void WriteMessage(Message message, string fileName)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (XmlWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.WriteMessage(writer);// write the message into a file&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Process.Start(fileName);// show the file&nbsp; &nbsp; }和序列化的消息。<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>
打开App,查看更多内容
随时随地看视频慕课网APP