Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Downloading and parsing an xml file

Posted on 2005-04-06
11
Medium Priority
?
249 Views
Last Modified: 2010-04-23
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
Comment
Question by:watersidedesigns
  • 6
  • 3
  • 2
11 Comments
 
LVL 8

Accepted Solution

by:
mani_sai earned 1000 total points
ID: 13716519
0
 
LVL 8

Expert Comment

by:mani_sai
ID: 13716551
also for uploading and downloading a file from website:



http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=115
0
 

Author Comment

by:watersidedesigns
ID: 13716751
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:b1xml2
ID: 13717097
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
 
LVL 23

Assisted Solution

by:b1xml2
b1xml2 earned 1000 total points
ID: 13717193
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
 
LVL 8

Expert Comment

by:mani_sai
ID: 13717226
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13717359
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13717413
didnt see mani's post
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13717431
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13717439
replacer Server.MapPath if you with path file if you are using Winforms
0
 

Author Comment

by:watersidedesigns
ID: 13718214
Thanks guys- got it :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question