0

我正在使用以下 VB.Net 代码使用 VB.NET 下载大型 XML 文件(64 MB)并将其保存在本地,效果很好,XML 提要如下所示。

我的 xml 提要网址是http://www.topbuy.com.au/tbcart/tbadmin/datafeed/shoppingcom.xml

我正在尝试实现并需要建议,而不是下载文件并将其存储在本地,VB.Net 中有没有一种方法可以读取标签之间的 XML 数据块并将字段插入 SQL 数据库。任何例子,我应该在下面的代码中改变什么(VB.Net)

Dim remoteUri As String = "http://www.topbuy.com.au/tbcart/tbadmin/datafeed/"
Dim fileName As String = "shoppingcom.xml"
Dim myStringWebResource As String = Nothing
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
' Concatenate the domain with the Web resource filename. Because DownloadFile 
'requires a fully qualified resource name, concatenate the domain with the Web resource file name.
myStringWebResource = remoteUri + fileName
Console.WriteLine("Downloading File ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)
' The DownloadFile() method downloads the Web resource and saves it into the current file-system folder.
myWebClient.DownloadFile(myStringWebResource, fileName)
Console.WriteLine("Successfully Downloaded file ""{0}"" from ""{1}""", fileName, myStringWebResource)
Console.WriteLine((ControlChars.Cr + "Downloaded file saved in the following file system folder:" + ControlChars.Cr + ControlChars.Tab + Application.StartupPath))

XML

<Products>
<Product><MPN><![CDATA[INK-PE-009]]></MPN><Manufacturer><![CDATA[Epson]]></Manufacturer><ProductName><![CDATA[Epson T009 Colour Compatible Inkjet Cartridge]]></ProductName><ProductURL><![CDATA[http://www.topbuy.com.au/tbcart/pc/Epson-T009-Colour-Compatible-Inkjet-Cartridge-p3343.htm?utm_source=TopBuy_ShoppingCom&utm_content=&utm_medium=cpc&dismode=1&utm_campaign=TBDF-XX10421]]></ProductURL><ProductType><![CDATA[Compatible Ink Cartridges]]></ProductType><ImageURL><![CDATA[http://www2.topbuy.com.au/tbcart/pc/catalog/General/TBDF-XX10421_1.jpg]]></ImageURL><Price>4.09</Price><OriginalPrice>9</OriginalPrice><Category><![CDATA[Consumables->Compatible Ink Cartridges]]></Category><ProductDescription><![CDATA[$4.05 Cash Price see store for detailsRelated Brand  EpsonOriginal Cartridge Equivalent T009Related Printers STYLUS 1270, STYLUS 1280, STYLUS 1290, STYLUS 3300C, STYLUS PHOTO 1270, STYLUS PHOTO 1290, STYLUS PHOTO 1290 silverThis cartridge works in the following printers  Epson Stylus Photo 1270/1280Please check the name (code) of the cartridge in your printer before ordering to ensure that it matches the name of the cartridges you are ordering from us. In some instances a printer can take more than one cartridge type and ...]]></ProductDescription><Stock>Y</Stock><ShippingCost>10</ShippingCost><StockDescription>No.1 OZ SUPERSTORE AUS WARRANTY FAST SHIPPING</StockDescription><Condition>Brand New</Condition></Product>
<Product><MPN><![CDATA[INK-PE-013]]></MPN><Manufacturer><![CDATA[Epson]]></Manufacturer><ProductName><![CDATA[Epson T013 Black Compatible Inkjet Cartridge]]></ProductName><ProductURL><![CDATA[http://www.topbuy.com.au/tbcart/pc/Epson-T013-Black-Compatible-Inkjet-Cartridge-p3345.htm?utm_source=TopBuy_ShoppingCom&utm_content=&utm_medium=cpc&dismode=1&utm_campaign=TBDF-XX10423]]></ProductURL><ProductType><![CDATA[Compatible Ink Cartridges]]></ProductType><ImageURL><![CDATA[http://www2.topbuy.com.au/tbcart/pc/catalog/General/TBDF-XX10423_1.jpg]]></ImageURL><Price>2.09</Price><OriginalPrice>5</OriginalPrice><Category><![CDATA[Consumables->Compatible Ink Cartridges]]></Category><ProductDescription><![CDATA[$2.07 Cash Price see store for detailsRelated Brand  EpsonOriginal Cartridge Equivalent T013Related Printers STYLUS COLOR 480, STYLUS COLOR 580, STYLUS COLOR C20, STYLUS COLOR C40, STYLUS COLOUR 480, STYLUS COLOUR 580, STYLUS COLOUR C20UX, STYLUS COLOUR C40SX, STYLUS COLOUR C40UXThis cartridge works in the following printers  Epson Stylus Colour 480/580Please check the name (code) of the cartridge in your printer before ordering to ensure that it matches the name of the cartridges you are ordering from us. In some instances a ...]]></ProductDescription><Stock>Y</Stock><ShippingCost>10</ShippingCost><StockDescription>No.1 OZ SUPERSTORE AUS WARRANTY FAST SHIPPING</StockDescription><Condition>Brand New</Condition></Product>
</Products>
4

1 回答 1

0

将 XmlReader 类与流一起使用,例如(对 C# 示例表示歉意,但我的 C# 比我的 VB 更好):

        WebRequest webRequest = HttpWebRequest.Create(uri);
        using(Stream stream = (await webRequest.GetResponseAsync()).GetResponseStream())
        using(XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings() { Async = true }))
        {
            // Use the XmlReader to move through the elements of the stream
        }

这将根据需要拉取数据,而无需将其存储在本地。如果你找到了你要找的东西,你只会下载和解析你需要的尽可能多的流。

请注意,此示例使用await关键字,因此应该在async方法中。

于 2012-09-03T15:07:35.247 回答