Error loading XML in XDocument

2

I'm consuming a WebService with the following code

WebRequest request = WebRequest.Create(link);

byte[] bytesToWrite = Encoding.ASCII.GetBytes(xml);

request.Method = "POST";
request.ContentLength = bytesToWrite.Length;
request.ContentType = "text/xml";
request.Headers.Add("SOAPAction", link);

Stream newStream = request.GetRequestStream();
newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
newStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

var xmlDoc = XDocument.Load(reader);

If I save the answer to a file I can read normally, var xmlDoc = XDocument.Load(@"C:\temp\Arquivo.xml");

If I try to load direct

var xmlDoc = XDocument.Load(reader);

The following error appears:

  

System.Xml.XmlException was unhandled     HResult = -2146232000     LineNumber = 0     LinePosition = 0     Message = Root element does not exist.     Source = System.Xml     SourceUri=""    StackTrace:          in System.Xml.XmlTextReaderImpl.Throw (Exception e)          at System.Xml.XmlTextReaderImpl.ParseDocumentContent ()          at System.Xml.XmlTextReaderImpl.Read ()          in System.Xml.Linq.XDocument.Load (XmlReader reader, LoadOptions options)          at System.Xml.Linq.XDocument.Load (TextReader textReader, LoadOptions options)          in System.Xml.Linq.XDocument.Load (TextReader textReader)

    
asked by anonymous 05.08.2016 / 20:00

0 answers