Q:

There is an error in XML document (2, 2): When trying to Deserialize this SOAP xml?

I have a soap message shown below. I would like to get only request element and its child nodes.
<?xml version="1.0" encoding="UTF-8" ?>
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:header>
    <htngheader xmlns="http://htng.org/1.1/Header/">
      <From>
        <systemId>abc</systemId>
        <address>abc/rm12.12</address>
        <Credential>
          <userName>xyz002</userName>
          <password>xyz002</password>
        </Credential>
      </From>
      <To>
        <systemID>PMM1</systemID>
      </To>
      <timeStamp>2009-04-23T10:59:00-04:00</timeStamp>
      <echoToken>9715f855-bd64-8e3e-905b-aa7ff227af2f</echoToken>
      <transactionId>760873954047</transactionId>
      <action>Request</action>
    </htngheader>
  </soap:header>
  <soap:body>
    <OTA_HotelInvCountNotifRQ echotoken="8c26af40-d777-a21b-ec7e-8d4a8d548a2d" 
                              timestamp="2011-04-23T10:59:00-04:00"
                              target="Production" 
                              version="1.002" sequencenmbr="760871059"
                              xmlns="http://www.example.com/OTA/2003/05"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Inventories HotelCode="ILP002" 
                   HotelName="Blue Lagoon Suites">
        <Inventory>
          <StatusApplicationControl Start="2011-08-03" End="2011-08-03"
                                    InvTypeCode="QU" Override="true"/>
          <InvCounts>
            <InvCount CountType="1" Count="0"/>
          </InvCounts>
        </Inventory>
      </Inventories>
    </OTA_HotelInvCountNotifRQ>
  </soap:body>
</soap:envelope>

When I try to deserialize above message it give me following exception:

An exception of type 'System.InvalidOperationException' occurred in
  System.Xml.dll but was not handled in user code
Additional information: There is an error in XML document (2, 2).

Here is my Deserealize Method:
private OTA_HotelInvCountNotifRQ DeserializeResXMl(string xmlbody)
{

    OTA_HotelInvCountNotifRQ ret = null;

    if (xmlbody != null && xmlbody != string.Empty)
    {
        try
        {
            long TimeStart = DateTime.Now.Ticks;

            XmlSerializer serializer;
            serializer = new XmlSerializer(typeof(OTA_HotelInvCountNotifRQ));
            ret = (OTA_HotelInvCountNotifRQ)serializer.Deserialize(new StringReader(xmlbody));
        }
        catch (Exception e)
        {
           ret = null;
        }
   }
   return ret;
}

Code Update:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/OTA/2003/05")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.opentravel.org/OTA/2003/05", IsNullable = false)]
public partial class OTA_HotelInvCountNotifRQ
{
    private InvCountType inventoriesField;
    private string echoTokenField;
    private System.DateTime timeStampField;
    private OTA_HotelInvCountNotifRQTarget targetField;
    private decimal versionField;

    public OTA_HotelInvCountNotifRQ()
    {
        this.targetField = OTA_HotelInvCountNotifRQTarget.Production;
    }

    /// <remarks/>
    public InvCountType Inventories
    {
        get
        {
            return this.inventoriesField;
        }
        set
        {
            this.inventoriesField = value;
        }
    }     
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string EchoToken
    {
        get
        {
            return this.echoTokenField;
        }
        set
        {
            this.echoTokenField = value;
        }
    }       
}

How can I fix this?

A:

Try following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            try
            {
                OTA_HotelInvCountNotifRQ ota_HotelInvCountNotifRQ = new OTA_HotelInvCountNotifRQ();
                string envelope = File.ReadAllText(FILENAME);

                OTA_HotelInvCountNotifRQ  serializedData =  ota_HotelInvCountNotifRQ.DeserializeResXMl(envelope);
            }
            catch (Exception e)
            {
                string message = e.Message;
                string stacktrace = e.StackTrace;
            }
        }
    } 
    [XmlRoot( ElementName = "envelope")]
    public class Envelope
    {
        [XmlElement(ElementName = "body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body body { get; set; }
    }
    [XmlRoot(ElementName = "body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "OTA_HotelInvCountNotifRQ", Namespace = "http://www.example.com/OTA/2003/05")]
        public OTA_HotelInvCountNotifRQ ota_HotelInvCountNotifRQ { get; set; }
    }

    [XmlRoot(ElementName = "OTA_HotelInvCountNotifRQ", Namespace = "http://www.example.com/OTA/2003/05")]
    public partial class OTA_HotelInvCountNotifRQ
    {
        private InvCountType inventoriesField;
        private string echoTokenField;
        private System.DateTime timeStampField;
        private OTA_HotelInvCountNotifRQTarget targetField;
        private decimal versionField;

        public OTA_HotelInvCountNotifRQ()
        {
            this.targetField = OTA_HotelInvCountNotifRQTarget.Production;
        }

        /// <remarks/>
        public InvCountType Inventories
        {
            get
            {
                return this.inventoriesField;
            }
            set
            {
                this.inventoriesField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string EchoToken
        {
            get
            {
                return this.echoTokenField;
            }
            set
            {
                this.echoTokenField = value;
            }
        }
        public OTA_HotelInvCountNotifRQ DeserializeResXMl(string envelopeStr)
        {

            OTA_HotelInvCountNotifRQ ret = null;

            if (envelopeStr != null && envelopeStr != string.Empty)
            {
                try
                {
                    long TimeStart = DateTime.Now.Ticks;

                    XmlSerializer serializer;
                    //serializer = new XmlSerializer(typeof(OTA_HotelInvCountNotifRQ));
                    serializer = new XmlSerializer(typeof(Envelope));
                    Envelope  envelope =  (Envelope)serializer.Deserialize(new StringReader(envelopeStr));
                    ret = envelope.body.ota_HotelInvCountNotifRQ;
                }
                catch (Exception e)
                {
                    ret = null;
                }
            }
            return ret;
        }
    }
    public class InvCountType
    {
    }
    public class OTA_HotelInvCountNotifRQTarget
    {
        public static OTA_HotelInvCountNotifRQTarget Production { get; set; }
    }
}

I made a couple of changes to xml
<?xml version="1.0" encoding="UTF-8" ?>
<envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:header>
    <htngheader xmlns="http://htng.org/1.1/Header/">
      <From>
        <systemId>abc</systemId>
        <address>abc/rm12.12</address>
        <Credential>
          <userName>xyz002</userName>
          <password>xyz002</password>
        </Credential>
      </From>
      <To>
        <systemID>PMM1</systemID>
      </To>
      <timeStamp>2009-04-23T10:59:00-04:00</timeStamp>
      <echoToken>9715f855-bd64-8e3e-905b-aa7ff227af2f</echoToken>
      <transactionId>760873954047</transactionId>
      <action>Request</action>
    </htngheader>
  </soap:header>
  <soap:body>
    <OTA_HotelInvCountNotifRQ echotoken="8c26af40-d777-a21b-ec7e-8d4a8d548a2d" 
                              timestamp="2011-04-23T10:59:00-04:00"
                              target="Production" 
                              version="1.002" sequencenmbr="760871059"
                              xmlns="http://www.example.com/OTA/2003/05"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Inventories HotelCode="ILP002" 
                   HotelName="Blue Lagoon Suites">
        <Inventory>
          <StatusApplicationControl Start="2011-08-03" End="2011-08-03"
                                    InvTypeCode="QU" Override="true"/>
          <InvCounts>
            <InvCount CountType="1" Count="0"/>
          </InvCounts>
        </Inventory>
      </Inventories>
    </OTA_HotelInvCountNotifRQ>
  </soap:body>
</envelope>

