Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading and writing xml problem

Status
Not open for further replies.

drew10

Programmer
Feb 26, 2002
123
US
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:

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()
 
what's this line in your try?

writer.Write(strPost)


where is strPost declared, and what does it contain?
penny1.gif
penny1.gif
 
dim strPost as Object = Request.Form

It contains a NameValueCollection that looks like a querystring.

What it does is send the contents of the form from another page to a given path's httpwebrequest.
 
Instead of saving the xml as a file and then loading the file, I tried this, but it gave me the same error:

Dim xmlDox as XmlDocument = New XmlDocument()
xmlDoc.LoadXml(result)

 
I've narrowed the problem down. The extra space in the xml document comes from the httpWebResponse. I have tried a replace of chr(10) which is linefeed, but it adds boxy characters that are still seen as new lines with:
result.replace(chr(10), string.empty) or result.replace(vblf, "").

The entire problem comes from several linefeeds before the xml. The httpWebResponse comes from another company, so I cannot change it. Any ideas are greatly appreciated!

-drew
 
try with chr(13)'s as well.

If that doesn't work, then I would suggest starting back over on that file, and reading each line. If it's empty, then discard it... if it's not, then write it to a new file, which will become the final file w/o the empty lines.

I think the chr(13) should work, though.
penny1.gif
penny1.gif
 
Unfortunately, chr(13) does not work. It does not remove anything from the string. When I open the xml document in notepad, all the text appears in the middle of the page, and if I move the cursor up, it moves line by line.

I am going to try to read the document line by line and write to a new document if the line is not blank.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top