I have a streamreader that reads xml from a HttpWebResponse.
I use a streamwriter to create and write xml to a new file. My problem occurs when I try to load the new xml document that I create. I get an error message saying "This is an unexpected XML declaration. Line 21, position 3." because when the file is created, the xml is not written until line 21. If I remove all the space above the xml in the file, it loads just fine. Does anyone know what I am doing wrong? Here's my code:
I use a streamwriter to create and write xml to a new file. My problem occurs when I try to load the new xml document that I create. I get an error message saying "This is an unexpected XML declaration. Line 21, position 3." because when the file is created, the xml is not written until line 21. If I remove all the space above the xml in the file, it loads just fine. Does anyone know what I am doing wrong? Here's my code:
Code:
Dim writer As StreamWriter
Dim req As HttpWebRequest = WebRequest.Create(url)
req.Method = "Post"
req.ContentType = "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL]
Try
writer = New StreamWriter(req.GetRequestStream())
writer.Write(strPost)
Finally
writer.Close()
End Try
'--------------- Get the response as xml ---------------------------
Dim resp As HttpWebResponse = req.GetResponse()
Dim sr As New StreamReader(resp.GetResponseStream())
result = Trim(sr.ReadToEnd)
'--------------- Write XML to file ----------------------------------
Dim sw As New StreamWriter(path)
sw.Write(result)
sw.Close()