开发者

How do I "Read" XML from Internet in Visual Basic .NET?

So I'm creating a program in Visual Basic 2010 that will be going through about 1.2 million XML files from the internet. The URL of each fil开发者_运维技巧e is in the following format:

website.com/xmlfeed.action?number=VARIABLE

where "VARIABLE" is a number between 1 and 1.2 million. Once I have the document, I need one specific value from the XML. The relevant section of the document tree is in the following format:

<XMLResponce>
 <SectionA>
   <SectionB>
    <Value> 250 </Value>
  </SectionB>
 </SectionA>
</XMLResponce>

where the data I need to extract is in "Value".

I spent awhile searching the web today, but came up empty handed. Everything I found was either irrelevant or FAR too complex for me to understand. I think the problem is that I'm not familiar with the terminology associated with XML.

I'd really appreciate an easy-to-understand solution or a link to an easy-to-understand solution. If it's not too much trouble, please explain how it works because I'm trying to learn.


To download a copy of the file you can use HttpWebRequest. You can put calls to GetResponse (or BeginGetResponse if you would prefer them to be asynchronous) in a loop where you generate the URL based on an incrementing Integer.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

To parse the file there are lots of options but Linq to Xml is probably a good bet.

http://msdn.microsoft.com/en-us/library/bb387098.aspx

Dim requestPrefix = "website.com/xmlfeed.action?number="

For documentNumber = 1 To 1200000
    Dim request = WebRequest.Create(requestPrefix & documentNumber)
    request.Timeout = 5000
    Dim response = request.GetResponse()

    Using stream = response.GetResponseStream()
        Dim xDocument = XDocument.Load(stream)

        'Use Linq to Xml to get the value you are after from the XDocument.
    End Using
Next


LINQ to XML in VB makes this quite painless. The following is not tested, so YMMV:

Dim values = From index in Enumerable.Range(1, 2100000)
             From doc in XDocument.Load(New Uri("http://website.com/xmlfeed.action?number=" & index))
             Select value in doc...<Value>.FirstOrDefault()

This will give you an IEnumerable with the first value node in each xml. Depending on the size of your XML, you may need to alter this method because XDocument.Load does load the entire xml document into memory before processing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜