Error loading XML in Xdocument

Asked

Viewed 835 times

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 in a file I can read normally, var xmlDoc = XDocument.Load(@"C:\temp\Arquivo.xml");

If I try to charge straight

var xmlDoc = XDocument.Load(reader);

The following error appears :

System.xml.Xmlexception was unhandled Hresult=-2146232000 Linenumber=0 Lineposition=0 Message=Missing root element. Source=System.Xml Sourceuri="" Stacktrace: in System.xml.Xmltextreaderimpl.Throw(Exception e) in System.xml.Xmltextreaderimpl.Parsedocumentcontent() in System.xml.Xmltextreaderimpl.Read() in System.xml.Linq.XDocument.Load(Xmlreader Reader, Loadoptions options) in System.xml.Linq.XDocument.Load(Textreader textReader, Loadoptions options) in System.xml.Linq.XDocument.Load(Textreader textReader)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.