views:

550

answers:

2

Hi, friends i am able to get xml file by sing bytes, perhaps which is getting some problem, can u suggest me alternate method to do the same thing to save xml file.

  Try
        Dim strUrl As String = "http://xyz.com" 
        Dim wr As HttpWebRequest = CType(WebRequest.Create(strUrl), HttpWebRequest)
        Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
        ws.ContentType = "UTF-16"
        Dim str As Stream = ws.GetResponseStream()
        Dim inBuf(100000) As Byte
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        While bytesToRead > 0
            Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
            If n = 0 Then
                Exit While
            End If
            bytesRead += n
            bytesToRead -= n
        End While
        Dim fstr As New FileStream("c:/GetXml.xml", FileMode.OpenOrCreate, FileAccess.Write)
        fstr.Write(inBuf, 0, bytesRead)
        str.Close()
        fstr.Close()
    Catch ex As WebException
        Response.Write(ex.Message)
    End Try
A: 

Consider using XMLTextReader. This example just loads the entire XML into a string, but obviously you could write it to a file instead:

    Dim strUrl As String = "http://xyz.com"
    Dim reader As XmlTextReader = New XmlTextReader(strUrl)
    Dim output as String

    Do While (reader.Read())
        Select Case reader.NodeType
            Case XmlNodeType.Element 

                Output = Output + "<" + reader.Name

                If reader.HasAttributes Then 
                    While reader.MoveToNextAttribute()
                        Output = Output + " {0}='{1}'", reader.Name, reader.Value)
                    End While
                End If
                Output = Output + ">"
            Case XmlNodeType.Text
                Output = Output + reader.Value
            Case XmlNodeType.EndElement
                Output = Output + "</" + reader.Name + ">"
        End Select
    Loop
Rip Rowan
+2  A: 

Why not just use the WebClient class and its DownloadFile method?? Seems a lot easier....

This is in C#, but you should have no trouble converting that to VB.NET:

WebClient wc = new WebClient();
wc.DownloadFile("http://xyz", @"C:\getxml.xml");

and you're done!

Marc

marc_s