如何使用 vb.NET 从 URL 读取 XML 数据并保存

发布于 2024-08-07 11:21:44 字数 1031 浏览 5 评论 0原文

朋友们,我能够通过单个字节获取 XML 文件,也许这遇到了一些问题。您可以建议我使用替代方法来执行相同的操作来保存 XML 文件吗?

  Try
        Dim strUrl As String = "http://example.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

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://example.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我是有多爱你 2024-08-14 11:21:44

为什么不直接使用 WebClient 类及其 DownloadFile 方法?看起来容易多了......

这是用 C# 编写的,但是将其转换为 VB.NET 应该没有问题:

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

然后就完成了!

马克

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

素手挽清风 2024-08-14 11:21:44

考虑使用 XMLTextReader。此示例只是将整个 XML 加载到字符串中,但显然您可以将其写入文件:

    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

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
冰雪梦之恋 2024-08-14 11:21:44

如果服务将请求发送到我们的 URL 怎么办?我如何调整它来读取他们发送的http流?经历了这么艰难的时光......(我应该做一个单独的线程吗?抱歉。)

What if the service is sending the request to our URL? How do I adjust this to read the http stream they send? Having such a hard time... (Should I do a separate thread? Sorry.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文