如何使用vb.NET从URL读取XML数据并保存 [英] How to read XML data from a URL by using vb.NET and save
本文介绍了如何使用vb.NET从URL读取XML数据并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友,我能够通过唱歌字节来获取XML文件,这也许会引起一些问题.您能建议我另一种方法来做同样的事情来保存XML文件吗?
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
推荐答案
为什么不只使用WebClient
类及其DownloadFile
方法?似乎容易得多....
Why not just use the WebClient
class and its DownloadFile
method?? Seems a lot easier....
这在C#中,但是将其转换为VB.NET应该没有问题:
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");
您就完成了!
马克
这篇关于如何使用vb.NET从URL读取XML数据并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文