• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Downloading and parsing an xml file

Dear experts,

I have kind of been thrown into the deep end and require some assistance.  I need to know how to download an xml file from a website and then analise the contents using vb.net.

This is my first vb.net project having used vb6 for many years it has come as a bit of a shock.  Any help would be grately appreciated.

Thanks
0
watersidedesigns
Asked:
watersidedesigns
  • 6
  • 3
  • 2
2 Solutions
 
mani_saiCommented:
0
 
mani_saiCommented:
also for uploading and downloading a file from website:



http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=115
0
 
watersidedesignsAuthor Commented:
Thanks for your quick reply Mani,

I added the following code to a button:

        Dim wc As New System.Net.WebClient
        Dim strm As System.IO.Stream

        strm = wc.OpenRead("http://www.watersidedesigns.net/current.xml")

        Dim sr = New System.IO.StreamReader(strm)

Whilst this appears to run when you quickfind on strm you can see a System.notsupported exception

any ideas?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
b1xml2Commented:
please read up on what constitutes a proper xml document.
http://www.w3schools.com/xml/

1. your document is invalid.
2. pls readup on what xml is.
2. do not copy and paste xml documents from MSIE. You should use view source.

your xml shd be like this
===================
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- This document is intended only for use by authorized licensees of The Weather Channel.
Unauthorized use is prohibited. Copyright 1995-2004, The Weather Channel Enterprises, Inc. All Rights Reserved.
-->
<weather ver="2.0">
      <head>
            <locale>en_US</locale>
            <form>MEDIUM</form>
            <ut>C</ut>
            <ud>km</ud>
            <us>km/h</us>
            <up>mb</up>
            <ur>mm</ur>
      </head>
      <loc id="UKXX0155">
            <dnam>Whitstable, United Kingdom</dnam>
            <tm>9:00 AM</tm>
            <lat>51.36</lat>
            <lon>1.03</lon>
            <sunr>6:22 AM</sunr>
            <suns>7:36 PM</suns>
            <zone>1</zone>
      </loc>
      <cc>
            <lsup>4/5/05 8:50 AM Local Time</lsup>
            <obst>Southend-On-Sea, United Kingdom</obst>
            <tmp>8</tmp>
            <flik>6</flik>
            <t>Fair</t>
            <icon>34</icon>
            <bar>
                  <r>1,025.1</r>
                  <d>steady</d>
            </bar>
            <wind>
                  <s>13</s>
                  <gust>N/A</gust>
                  <d>240</d>
                  <t>WSW</t>
            </wind>
            <hmid>76</hmid>
            <vis>10.0</vis>
            <uv>
                  <i>1</i>
                  <t>Low</t>
            </uv>
            <dewp>4</dewp>
            <moon>
                  <icon>25</icon>
                  <t>Waning Crescent</t>
            </moon>
      </cc>
 </weather>
0
 
b1xml2Commented:
an example of loading xml from the web into an xmldocument

Dim client As New WebClient
Dim stm As Stream = client.OpenRead("http://p.moreover.com/page?o=rss002&c=Top%20stories")
Dim doc As New XmlDocument
doc.Load(stm)
doc.Save(...)

0
 
mani_saiCommented:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileContents As String = ReadFromFile(Server.MapPath("TestFile.txt"))
        Dim webPageContents As String = ReadFromUrl("http://weblogs.asp.net/DNeimke")

        PlaceHolder1.Controls.Add(New LiteralControl(Server.HtmlEncode(fileContents)))
        PlaceHolder2.Controls.Add(New LiteralControl(Server.HtmlEncode(webPageContents)))
    End Sub

    Public Function ReadFromFile(ByVal filename As String) As String
        Dim fs As FileStream = File.OpenRead(filename)
        Dim sr As StreamReader = New StreamReader(fs)
        ReadFromFile = sr.ReadToEnd
        sr.Close()
        fs.Close()
    End Function

    Public Function ReadFromUrl(ByVal url As String) As String
        Dim wc As New WebClient()
        Dim s As Stream = wc.OpenRead(url)
        Dim sr As StreamReader = New StreamReader(s)
        ReadFromUrl = sr.ReadToEnd
        s.Close()
        wc.Dispose()
    End Function
0
 
b1xml2Commented:
another example:

Dim client As New WebClient
Dim stm As Stream = client.OpenRead("http://p.moreover.com/page?o=rss002&c=Top%20stories")
Dim rdr As New StreamReader(stm)
Dim wt As New StreamWriter("c:\data.xml")
wt.Write(rdr.ReadToEnd())
wt.Flush()
0
 
b1xml2Commented:
didnt see mani's post
0
 
b1xml2Commented:
further refinement...

mani's post doesnt deal with saving the file...
 
Dim client As New WebClient
Dim stm As Stream = client.OpenRead("http://p.moreover.com/page?o=rss002&c=Top%20stories")
Dim rdr As New StreamReader(stm)
Dim wt As New StreamWriter(Server.MapPath("i.xml"))
wt.Write(rdr.ReadToEnd())
wt.Flush()
wt.Close()
rdr.Close()
stm.Close()
client.Dispose()
0
 
b1xml2Commented:
replacer Server.MapPath if you with path file if you are using Winforms
0
 
watersidedesignsAuthor Commented:
Thanks guys- got it :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now