Question reading a xml file from webserver (NOT console)

peroja

Member
Joined
May 16, 2012
Messages
7
Programming Experience
1-3
Hello,

I try to resolve a little problem. I want to build a very simple programm. A Windows Form Application with just one Label (this is just 4fun / education how to do it)
The Label should text out a Name or something from a XML file on my Webserver.

I found a nice "Console" Application which works just fine but i consider myself still as a beginner... So, Console is not Form Application.

This Works fine. But how can i output strings on Labels, Textboxes etc...?

PLS as simple as possible.
Thank you.

VB.NET:
Imports System.Net
Imports System.IO
Imports System.Xml

Module Module1

    Sub Main()
        Dim URL As String
        URL = "http://www.w3schools.com/xml/note.xml"

        Try
            Dim Request As WebRequest = HttpWebRequest.Create(URL)
            Dim Response As WebResponse = Request.GetResponse
            Dim sReader As New StreamReader(Response.GetResponseStream())

            Dim xmlDoc As New XmlDocument()
            xmlDoc.Load(sReader)
            Dim TitleNode As XmlNodeList = xmlDoc.GetElementsByTagName("note")

            Console.WriteLine("To: " + TitleNode.Item(0).Item("to").InnerText)
            Console.WriteLine("From: " + TitleNode.Item(0).Item("from").InnerText)
            Console.WriteLine("Heading: " + TitleNode.Item(0).Item("heading").InnerText)
            Console.WriteLine("Body: " + TitleNode.Item(0).Item("body").InnerText)
            Console.ReadKey()

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadKey()
        End Try
    End Sub

End Module
 
Remove the Console.WriteLine call and what is left? Some Xml code and a few result strings. Those strings you can assign to your Label.Text property.
You don't need the WebRequest/WebResponse/StreamReader by the way, XmlDocument will load just fine directly from the url.
 
hi john

you mean like this? i tried but that does not work? i might missunderstood you...?

VB.NET:
Imports System.Net
Imports System.IO
Imports System.Xml

Public Class Form1
    Sub Main()
        Dim URL As String
        URL = "http://www.w3schools.com/xml/note.xml"

        Try
            Dim Request As WebRequest = HttpWebRequest.Create(URL)
            Dim Response As WebResponse = Request.GetResponse
            Dim sReader As New StreamReader(Response.GetResponseStream())

            Dim xmlDoc As New XmlDocument()
            xmlDoc.Load(sReader)
            Dim TitleNode As XmlNodeList = xmlDoc.GetElementsByTagName("note")

            TextBox1.Text = TitleNode.Item(0).Item("to").InnerText
            

        Catch ex As Exception
            End Try
    End Sub
End Class
 
Then you take the content of that sub and put where you want it to be executed.
 
You can for example doubleclick the form to get to the Load event handler where you add the code.
 
I also see you use .Net 4, so you should look into more moderns ways like Linq to Xml. XML in Visual Basic
Here's an example getting the <note>.<heading> value:
        Dim doc = XDocument.Load("http://www.w3schools.com/xml/note.xml")
        Me.Label1.Text = doc.<note>.<heading>.Value
 
Back
Top