title: Getting XML from stored procedure
body: <p>I am getting an output of XML from a stored procedure. What I am trying to do is get that XML and pass it out via ASP.NET:</p>
  
  <pre><code>public XmlDocument GetPunchListXml(string communityDesc)
  {
      try
      {
          using (connection = new SqlConnection(connectionString))
          {
               connection.Open();
  
               using (SqlCommand command = new SqlCommand("GetPunchList", connection))
               {
                     command.CommandType = CommandType.StoredProcedure;
  
                     SqlParameter parameter1 = new SqlParameter("@communityDesc", SqlDbType.VarChar);
                     parameter1.Value = communityDesc;
                     parameter1.Direction = ParameterDirection.Input;
                     command.Parameters.Add(parameter1);
  
                     var doc = new XmlDocument();
                     var reader = command.ExecuteXmlReader();
                     if (reader.Read())
                     {
                         doc.Load(reader);
                     }
  
                     return doc;
                 }
             }
        }
        finally
        {
           connection.Close();
        }
  }
  </code></pre>
  
  <p>but I keep getting these errors:</p>
  
  <pre><code>&lt;Error&gt;
      &lt;Message&gt;An error has occurred.&lt;/Message&gt;
      &lt;ExceptionMessage&gt;
          The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
      &lt;/ExceptionMessage&gt;
      &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
      &lt;StackTrace/&gt;
      &lt;InnerException&gt;
          &lt;Message&gt;An error has occurred.&lt;/Message&gt;
          &lt;ExceptionMessage&gt;
              Type 'System.Xml.XmlDocument' is an invalid collection type since it does not have a valid Add method with parameter of type 'System.Object'.
          &lt;/ExceptionMessage&gt;
          &lt;ExceptionType&gt;
              System.Runtime.Serialization.InvalidDataContractException
          &lt;/ExceptionType&gt;
          &lt;StackTrace&gt;
              at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
              at WriteArrayOfanyTypeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
              at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
              at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
              at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
              at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
              at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
              at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
              at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
              at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)
              at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
                  --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
              at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
              at System.Web.Http.WebHost.HttpControllerHandler.&lt;WriteBufferedResponseContentAsync&gt;d__1b.MoveNext()
          &lt;/StackTrace&gt;
      &lt;/InnerException&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>What am I doing wrong? Is there something wrong with the way I am trying to get the XML or would be an issue with the XML itself?</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>var doc = new XmlDocument();
  string s = "";
  using (XmlReader reader = command.ExecuteXmlReader())
  {
      while (reader.Read())
      {
          //doc.Load(reader);
          s = reader.ReadOuterXml();
          doc.LoadXml(s);
      }
  }
  return doc;
  </code></pre>
  
  <p>and got these errors:</p>
  
  <pre><code>&lt;Error&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
  &lt;StackTrace/&gt;
  &lt;InnerException&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  Type 'System.Xml.XmlDocument' is an invalid collection type since it does not have a valid Add method with parameter of type 'System.Object'.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;
  System.Runtime.Serialization.InvalidDataContractException
  &lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type) at WriteArrayOfanyTypeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.WebHost.HttpControllerHandler.&lt;WriteBufferedResponseContentAsync&gt;d__1b.MoveNext()
  &lt;/StackTrace&gt;
  &lt;/InnerException&gt;
  &lt;/Error&gt;
  </code></pre>