2

友達、sing bytes で 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
4

3 に答える 3

7

WebClientクラスとそのメソッドを使用しないのはなぜDownloadFileですか?? はるかに簡単に思えます....

これは C# ですが、問題なく VB.NET に変換できます。

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

これで完了です。

マルク

于 2009-10-16T08:03:03.020 に答える
0

サービスがリクエストを URL に送信している場合はどうなるでしょうか。彼らが送信する http ストリームを読み取るようにこれを調整するにはどうすればよいですか? せっかくなので…(別スレにしようかな?すみません。)

于 2013-02-22T21:46:09.240 に答える
0

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
于 2009-10-16T07:48:46.887 に答える