无法以编程方式反序列化SOAP消息

当我尝试反序列化soap消息时,我得到以下exception。 我这样做是因为我有我想在测试中重用的响应文件。 我不能使用真实的服务或类似的东西,因为它不适合我们的测试框架的架构。

Test 'MyUnitTestMethod' failed: System.InvalidOperationException : There is an error in XML document (1, 2). ----> System.InvalidOperationException :  was not expected. at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle) at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream) private const string _content = @"   
0
name test1@mail.com "; /// [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xsd.com/msgs/v1")] public partial class MySpecialResponse : BaseResponse { private MySpecialPayload payloadField; /// public MySpecialPayload Payload { get { return this.payloadField; } set { this.payloadField;= value; } } } [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xsd.com/msgs/v1")] public partial class MySpecialPayload { private string customerNameField; private string emailAddressField; /// public string CustomerName { get { return this.customerNameField; } set { this.customerNameField = value; } } /// public string EmailAddress { get { return this.emailAddressField; } set { this.emailAddressField = value; } } } //The code I am using - might not be right? var serialiser = new System.Xml.Serialization.XmlSerializer(typeof(MySpecialResponse)); using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(_content)) { var doc = new System.Xml.XmlDocument(); doc.Load(stream); var nsManager = new System.Xml.XmlNamespaceManager(doc.NameTable); nsManager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); //so I can get the actual response and not the soap body var node = doc.SelectSingleNode("//soapenv:Body", nsManager); if (node != null) { byte[] xml = Encoding.UTF8.GetBytes(node.InnerXml); using (var memStream = new System.IO.MemoryStream(xml)) { memStream.Position = 0; var resp = (MySpecialResponse)serialiser.Deserialize(memStream); //Throws exception here } } }

(BaseResponse有标题字段)

任何想法我的exception都会被提出,以及我如何获取soap消息以将消息反序列化到对象中,无论是修复我的代码还是应用其他技术。

注意:我有许多文件,我想要一个反序列化的通用方法,所以我不会提前知道所有的xml命名空间,只有根响应类型。

谢谢

好的,我已经为自己解决了。

我需要为XmlSerializer添加XmlRootAttribute

 var xRoot = new System.Xml.Serialization.XmlRootAttribute(); xRoot.ElementName = "MySpecialResponse"; xRoot.IsNullable = true; xRoot.Namespace = rootResponseNamespace; var serialiser = new System.Xml.Serialization.XmlSerializer(typeof(MySpecialResponse), xRoot);