title: How to Convert HTTP Response XML Document into object in C#?
body: <p>I want to convert HTTP Response XML document into object.</p>
  
  <p>When I tried to Deserialize I am getting Error like </p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.InvalidOperationException'
    occurred in System.Xml.dll</p>
  </blockquote>
  
  <p>Additional information: There is an error in XML document (1, 2).</p>
  
  <p>Here is my Class File</p>
  
  <pre><code>namespace Salcomp.SerialNumberStatus
  {
  using System;
  using System.Diagnostics;
  using System.Xml.Serialization;
  using System.Collections;
  using System.Xml.Schema;
  using System.ComponentModel;
  using System.IO;
  using System.Text;
  using System.Collections.Generic;
  
  public partial class OperationTypes
  {
  
      private List&lt;OperationType&gt; operationTypeField;
  
      private static System.Xml.Serialization.XmlSerializer serializer;
  
      public OperationTypes()
      {
          this.operationTypeField = new List&lt;OperationType&gt;();
      }
  
      public List&lt;OperationType&gt; OperationType
      {
          get
          {
              return this.operationTypeField;
          }
          set
          {
              this.operationTypeField = value;
          }
      }
  
      private static System.Xml.Serialization.XmlSerializer Serializer
      {
          get
          {
              if ((serializer == null))
              {
                  serializer = new System.Xml.Serialization.XmlSerializer(typeof(OperationTypes));
              }
              return serializer;
          }
      }
  
      #region Serialize/Deserialize
      /// &lt;summary&gt;
      /// Serializes current OperationTypes object into an XML document
      /// &lt;/summary&gt;
      /// &lt;returns&gt;string XML value&lt;/returns&gt;
      public virtual string Serialize()
      {
          System.IO.StreamReader streamReader = null;
          System.IO.MemoryStream memoryStream = null;
          try
          {
              memoryStream = new System.IO.MemoryStream();
              Serializer.Serialize(memoryStream, this);
              memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
              streamReader = new System.IO.StreamReader(memoryStream);
              return streamReader.ReadToEnd();
          }
          finally
          {
              if ((streamReader != null))
              {
                  streamReader.Dispose();
              }
              if ((memoryStream != null))
              {
                  memoryStream.Dispose();
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Deserializes workflow markup into an OperationTypes object
      /// &lt;/summary&gt;
      /// &lt;param name="xml"&gt;string workflow markup to deserialize&lt;/param&gt;
      /// &lt;param name="obj"&gt;Output OperationTypes object&lt;/param&gt;
      /// &lt;param name="exception"&gt;output Exception value if deserialize failed&lt;/param&gt;
      /// &lt;returns&gt;true if this XmlSerializer can deserialize the object; otherwise, false&lt;/returns&gt;
      public static bool Deserialize(string xml, out OperationTypes obj, out System.Exception exception)
      {
          exception = null;
          obj = default(OperationTypes);
          try
          {
              obj = Deserialize(xml);
              return true;
          }
          catch (System.Exception ex)
          {
              exception = ex;
              return false;
          }
      }
  
      public static bool Deserialize(string xml, out OperationTypes obj)
      {
          System.Exception exception = null;
          return Deserialize(xml, out obj, out exception);
      }
  
      public static OperationTypes Deserialize(string xml)
      {
          System.IO.StringReader stringReader = null;
          try
          {
              stringReader = new System.IO.StringReader(xml);
              return ((OperationTypes)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
          }
          finally
          {
              if ((stringReader != null))
              {
                  stringReader.Dispose();
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Serializes current OperationTypes object into file
      /// &lt;/summary&gt;
      /// &lt;param name="fileName"&gt;full path of outupt xml file&lt;/param&gt;
      /// &lt;param name="exception"&gt;output Exception value if failed&lt;/param&gt;
      /// &lt;returns&gt;true if can serialize and save into file; otherwise, false&lt;/returns&gt;
      public virtual bool SaveToFile(string fileName, out System.Exception exception)
      {
          exception = null;
          try
          {
              SaveToFile(fileName);
              return true;
          }
          catch (System.Exception e)
          {
              exception = e;
              return false;
          }
      }
  
      public virtual void SaveToFile(string fileName)
      {
          System.IO.StreamWriter streamWriter = null;
          try
          {
              string xmlString = Serialize();
              System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
              streamWriter = xmlFile.CreateText();
              streamWriter.WriteLine(xmlString);
              streamWriter.Close();
          }
          finally
          {
              if ((streamWriter != null))
              {
                  streamWriter.Dispose();
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Deserializes xml markup from file into an OperationTypes object
      /// &lt;/summary&gt;
      /// &lt;param name="fileName"&gt;string xml file to load and deserialize&lt;/param&gt;
      /// &lt;param name="obj"&gt;Output OperationTypes object&lt;/param&gt;
      /// &lt;param name="exception"&gt;output Exception value if deserialize failed&lt;/param&gt;
      /// &lt;returns&gt;true if this XmlSerializer can deserialize the object; otherwise, false&lt;/returns&gt;
      public static bool LoadFromFile(string fileName, out OperationTypes obj, out System.Exception exception)
      {
          exception = null;
          obj = default(OperationTypes);
          try
          {
              obj = LoadFromFile(fileName);
              return true;
          }
          catch (System.Exception ex)
          {
              exception = ex;
              return false;
          }
      }
  
      public static bool LoadFromFile(string fileName, out OperationTypes obj)
      {
          System.Exception exception = null;
          return LoadFromFile(fileName, out obj, out exception);
      }
  
      public static OperationTypes LoadFromFile(string fileName)
      {
          System.IO.FileStream file = null;
          System.IO.StreamReader sr = null;
          try
          {
              file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
              sr = new System.IO.StreamReader(file);
              string xmlString = sr.ReadToEnd();
              sr.Close();
              file.Close();
              return Deserialize(xmlString);
          }
          finally
          {
              if ((file != null))
              {
                  file.Dispose();
              }
              if ((sr != null))
              {
                  sr.Dispose();
              }
          }
      }
      #endregion
  }
  
  public partial class OperationType
  {
  
      private string uriField;
  
      private string nameField;
  
      private string numberField;
  
      private bool activeField;
  
      private bool activeFieldSpecified;
  
      private string descriptionField;
  
      private OperationSetupCalculation setupCalculationField;
  
      private bool setupCalculationFieldSpecified;
  
      private string sideField;
  
      private bool usedForRoutingField;
  
      private bool usedForSchedulingField;
  
      private string categoryField;
  
      private string skillsField;
  
      private string aaa;
  
      private static System.Xml.Serialization.XmlSerializer serializer;
  
      public OperationType()
      {
          this.usedForRoutingField = false;
          this.usedForSchedulingField = false;
      }
  
      public string Uri
      {
          get
          {
              return this.uriField;
          }
          set
          {
              this.uriField = value;
          }
      }
  
      public string Name
      {
          get
          {
              return this.nameField;
          }
          set
          {
              this.nameField = value;
          }
      }
  
      public string Number
      {
          get
          {
              return this.numberField;
          }
          set
          {
              this.numberField = value;
          }
      }
  
      public bool Active
      {
          get
          {
              return this.activeField;
          }
          set
          {
              this.activeField = value;
          }
      }
  
      [System.Xml.Serialization.XmlIgnoreAttribute()]
      public bool ActiveSpecified
      {
          get
          {
              return this.activeFieldSpecified;
          }
          set
          {
              this.activeFieldSpecified = value;
          }
      }
  
      public string Description
      {
          get
          {
              return this.descriptionField;
          }
          set
          {
              this.descriptionField = value;
          }
      }
  
      public OperationSetupCalculation SetupCalculation
      {
          get
          {
              return this.setupCalculationField;
          }
          set
          {
              this.setupCalculationField = value;
          }
      }
  
      [System.Xml.Serialization.XmlIgnoreAttribute()]
      public bool SetupCalculationSpecified
      {
          get
          {
              return this.setupCalculationFieldSpecified;
          }
          set
          {
              this.setupCalculationFieldSpecified = value;
          }
      }
  
      public string Side
      {
          get
          {
              return this.sideField;
          }
          set
          {
              this.sideField = value;
          }
      }
  
      [System.ComponentModel.DefaultValueAttribute(false)]
      public bool UsedForRouting
      {
          get
          {
              return this.usedForRoutingField;
          }
          set
          {
              this.usedForRoutingField = value;
          }
      }
  
      [System.ComponentModel.DefaultValueAttribute(false)]
      public bool UsedForScheduling
      {
          get
          {
              return this.usedForSchedulingField;
          }
          set
          {
              this.usedForSchedulingField = value;
          }
      }
  
      public string Category
      {
          get
          {
              return this.categoryField;
          }
          set
          {
              this.categoryField = value;
          }
      }
  
      public string Skills
      {
          get
          {
              return this.skillsField;
          }
          set
          {
              this.skillsField = value;
          }
      }
  
      private static System.Xml.Serialization.XmlSerializer Serializer
      {
          get
          {
              if ((serializer == null))
              {
                  serializer = new System.Xml.Serialization.XmlSerializer(typeof(OperationType));
              }
              return serializer;
          }
      }
  
      #region Serialize/Deserialize
      /// &lt;summary&gt;
      /// Serializes current OperationType object into an XML document
      /// &lt;/summary&gt;
      /// &lt;returns&gt;string XML value&lt;/returns&gt;
      public virtual string Serialize()
      {
          System.IO.StreamReader streamReader = null;
          System.IO.MemoryStream memoryStream = null;
          try
          {
              memoryStream = new System.IO.MemoryStream();
              Serializer.Serialize(memoryStream, this);
              memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
              streamReader = new System.IO.StreamReader(memoryStream);
              return streamReader.ReadToEnd();
          }
          finally
          {
              if ((streamReader != null))
              {
                  streamReader.Dispose();
              }
              if ((memoryStream != null))
              {
                  memoryStream.Dispose();
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Deserializes workflow markup into an OperationType object
      /// &lt;/summary&gt;
      /// &lt;param name="xml"&gt;string workflow markup to deserialize&lt;/param&gt;
      /// &lt;param name="obj"&gt;Output OperationType object&lt;/param&gt;
      /// &lt;param name="exception"&gt;output Exception value if deserialize failed&lt;/param&gt;
      /// &lt;returns&gt;true if this XmlSerializer can deserialize the object; otherwise, false&lt;/returns&gt;
      public static bool Deserialize(string xml, out OperationType obj, out System.Exception exception)
      {
          exception = null;
          obj = default(OperationType);
          try
          {
              obj = Deserialize(xml);
              return true;
          }
          catch (System.Exception ex)
          {
              exception = ex;
              return false;
          }
      }
  
      public static bool Deserialize(string xml, out OperationType obj)
      {
          System.Exception exception = null;
          return Deserialize(xml, out obj, out exception);
      }
  
      public static OperationType Deserialize(string xml)
      {
          System.IO.StringReader stringReader = null;
          try
          {
              stringReader = new System.IO.StringReader(xml);
              return ((OperationType)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
          }
          finally
          {
              if ((stringReader != null))
              {
                  stringReader.Dispose();
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Serializes current OperationType object into file
      /// &lt;/summary&gt;
      /// &lt;param name="fileName"&gt;full path of outupt xml file&lt;/param&gt;
      /// &lt;param name="exception"&gt;output Exception value if failed&lt;/param&gt;
      /// &lt;returns&gt;true if can serialize and save into file; otherwise, false&lt;/returns&gt;
      public virtual bool SaveToFile(string fileName, out System.Exception exception)
      {
          exception = null;
          try
          {
              SaveToFile(fileName);
              return true;
          }
          catch (System.Exception e)
          {
              exception = e;
              return false;
          }
      }
  
      public virtual void SaveToFile(string fileName)
      {
          System.IO.StreamWriter streamWriter = null;
          try
          {
              string xmlString = Serialize();
              System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
              streamWriter = xmlFile.CreateText();
              streamWriter.WriteLine(xmlString);
              streamWriter.Close();
          }
          finally
          {
              if ((streamWriter != null))
              {
                  streamWriter.Dispose();
              }
          }
      }
  
      /// &lt;summary&gt;
      /// Deserializes xml markup from file into an OperationType object
      /// &lt;/summary&gt;
      /// &lt;param name="fileName"&gt;string xml file to load and deserialize&lt;/param&gt;
      /// &lt;param name="obj"&gt;Output OperationType object&lt;/param&gt;
      /// &lt;param name="exception"&gt;output Exception value if deserialize failed&lt;/param&gt;
      /// &lt;returns&gt;true if this XmlSerializer can deserialize the object; otherwise, false&lt;/returns&gt;
      public static bool LoadFromFile(string fileName, out OperationType obj, out System.Exception exception)
      {
          exception = null;
          obj = default(OperationType);
          try
          {
              obj = LoadFromFile(fileName);
              return true;
          }
          catch (System.Exception ex)
          {
              exception = ex;
              return false;
          }
      }
  
      public static bool LoadFromFile(string fileName, out OperationType obj)
      {
          System.Exception exception = null;
          return LoadFromFile(fileName, out obj, out exception);
      }
  
      public static OperationType LoadFromFile(string fileName)
      {
          System.IO.FileStream file = null;
          System.IO.StreamReader sr = null;
          try
          {
              file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
              sr = new System.IO.StreamReader(file);
              string xmlString = sr.ReadToEnd();
              sr.Close();
              file.Close();
              return Deserialize(xmlString);
          }
          finally
          {
              if ((file != null))
              {
                  file.Dispose();
              }
              if ((sr != null))
              {
                  sr.Dispose();
              }
          }
      }
      #endregion
  }
  </code></pre>
  
  <p>Another File:</p>
  
  <pre><code>    namespace Salcomp.SerialNumberStatus
      {
      public partial class Form1 : Form
      {
          MasterDataManagement MDM = new MasterDataManagement("http://localhost:8012/");
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              XmlDocument doc = new XmlDocument();
              Salcomp.SerialNumberStatus.OperationType operationType = new Salcomp.SerialNumberStatus.OperationType();
  
             var test =  MDM.RestGet&lt;Salcomp.SerialNumberStatus.OperationType&gt;("http://localhost/Enterprise/OperationTypes");
  
          }
      }
  }
  </code></pre>
  
  <p>Deserialize </p>
  
  <pre><code>public T RestGet&lt;T&gt;(string url)
      {
          if (!url.Contains("http://")) url = _mdmServerUrl + url;
          var xDoc = url.HttpGet();
          var xml = xDoc.ToString();
          return XmlDeserializeFromString&lt;T&gt;(xml);
      }
  
      private T XmlDeserializeFromString&lt;T&gt;( string objectData)
      {
          return (T)XmlDeserializeFromString(objectData, typeof(T));
      }
  
      private  object XmlDeserializeFromString( string objectData, Type type)
      {
          XmlSerializer serializer = new XmlSerializer(type);
          object result;
  
          using (TextReader reader = new StringReader(objectData))
          {
              result = serializer.Deserialize(reader);
          }
          return result;
      }
  </code></pre>
  
  <p>XML Response:</p>
  
  <pre><code>&lt;OperationTypes xmlns="http://www.example.com/aaa/2011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
   &lt;OperationType Uri="http://localhost:8012/Enterprise/OperationTypes/a1d02dac-c454-497b-808d-a619009c7c7e" Name="test" Number="19" Active="true" Description="" SetupCalculation="PerWorkOrder" Side="" /&gt;
  &lt;OperationType Uri="http://localhost:8012/Enterprise/OperationTypes/07102c89-93e1-499b-980e-a61900cf2217" Name="OT_Prakash" Number="20" Active="false" Description="" SetupCalculation="PerWorkOrder" Side="" /&gt;
  &lt;/OperationTypes&gt;
  </code></pre>
  
  <p>I have tried many answers in stackoverflow but I am not able to get it.</p>
  
  <p>any help appreciated.</p>
  
  <p>Thanks in Advance.</p>