Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5941495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:07:07+00:00 2026-05-22T16:07:07+00:00

I get the following exception when I try to deserialize a soap message. I

  • 0

I get the following exception when I try to deserialize a soap message. I am doing it this way cause I have the response files I want to reuse in testing. I cannot use a real service or the like as it does not fit the architecture for the testing framework we have.

    Test 'MyUnitTestMethod' failed: System.InvalidOperationException : There is an error in XML document (1, 2).
----> System.InvalidOperationException : <MySpecialResponse xmlns='http://xsd.com/msgs/v1'> 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 =
@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
  <soapenv:Body>
    <ns3:MySpecialResponse xmlns:ns3=""http://xsd.com/msgs/v1"" >
      <header>
        <status>0</status>
      </header>
      <ns3:Payload>
        <ns3:CustomerName>name</ns3:CustomerName>
        <ns3:EmailAddress>test1@mail.com</ns3:EmailAddress>
      </ns3:Payload>
    </ns3:MySpecialResponse>
  </soapenv:Body>
</soapenv:Envelope>";

/// <remarks/>
[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;

    /// <remarks/>
    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;

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

    /// <remarks/>
    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 has the header field)

Any ideas my the exception is getting raised and how I can get the soap message to deserialize the message into the object whether it be fixing my code or applying another technology.

Note: I have many files that I want to ue a generic method for deserializing so I will not know ahead of time all the xml namespaces, only the root response type.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:07:07+00:00Added an answer on May 22, 2026 at 4:07 pm

    Ok, I have solved it for myself.

    I needed to add the XmlRootAttribute for the XmlSerializer

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following exception when i try to execute specific stored procedure :
I get the following exception when I try to find and replace in a
When i try to hit ibatis, i get following exception. Whatis the issue. which
Why do I get the following exception when I did this, what mistake did
Q: I get the following exception when i try to execute the following query:
When i try use a webservice i get the following exception. My main question
I get the following exception when trying to access any nodes of a parsed
I get the following Exception running my app: java.net.SocketException: Permission denied (maybe missing INTERNET
I seemed to get the following exception when trying to deploy my application: Caused
I get the following error: Unhandled Exception: System.IO.IOException: The parameter is incorrect. at System.IO.__Error.WinIOError(Int32

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.