Labels text = xml value?

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
9 posts Page 1 of 1
Contributors
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Labels text = xml value?
zachman61
Say an xml file has
Code: Select all
<!-- Example Xml i made for example -->
<!-- Thanks in advance -->
<txt>Get the newest Update: http://EXAMPLE.com/update.zip</txt>
how could i have whats inside the "<txt>" tag show up as the label's text
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Labels text = xml value?
mandai
You could use this:
Code: Select all
        Dim xtr As XmlTextReader = New XmlTextReader("file.xml")
        Dim content As String = xtr.ReadElementString("txt")
        xtr.Close()
        Label1.Text = content
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: Labels text = xml value?
zachman61
Mandai, You should write a book on this :D
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: Labels text = xml value?
GoodGuy17
You've got me interested now, Zach :P

Hey Mandai, what if there is more than one txt tag?
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: Labels text = xml value?
zachman61
GoodGuy17 wrote:
You've got me interested now, Zach :P

Hey Mandai, what if there is more than one txt tag?
We'll im pretty sure you would need a timer and it would go back and forth between them
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Labels text = xml value?
mandai
You don't need a timer when the data stays the same.
In XML you aren't supposed to have root elements with the same name, but you could check for child txt nodes with this:
Code: Select all
        Dim xtr As XmlTextReader = New XmlTextReader("file.xml")

        While xtr.Read()

            If xtr.Name = "txt" Then
                Dim content As String = xtr.ReadString()
                MsgBox(content)
            End If
        End While
        xtr.Close()
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: Labels text = xml value?
zachman61
mandai wrote:
You don't need a timer when the data stays the same.
In XML you aren't supposed to have root elements with the same name, but you could check for child txt nodes with this:
Code: Select all
        Dim xtr As XmlTextReader = New XmlTextReader("file.xml")

        While xtr.Read()

            If xtr.Name = "txt" Then
                Dim content As String = xtr.ReadString()
                MsgBox(content)
            End If
        End While
        xtr.Close()
What do u mean by child?
like this?
Code: Select all
<txt>hi</txt>
<txt>hello</txt
do u mean that?
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Labels text = xml value?
mandai
It just means the txt elements are at least 1 level inside another element.
Like:
Code: Select all
<xml>
 <txt>value1</txt>
 <txt>value2</txt>
</xml>
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: Labels text = xml value?
Scottie1972
I have always used a code like this to read xml file.
Not sure if it is more confusing to you or not.
The XML File
Code: Select all
<xml>
  <user>
     <userid>1</userid>
     <first>First Name</first>
     <last>Last Name</last>
     <email>myEmail@somesite.com</email>
  </user>
  <user>
     <userid>2</userid>
     <first>First Name</first>
     <last>Last Name</last>
     <email>myEmail@somesite.com</email>
  </user>
</xml>
The reader Code
this code will loop through all the parent nodes "<user> ... </user>" and list all the entrys that it can find.
Code: Select all
           Try
                Dim XmlDoc As New XmlDocument
                'Location of Stored XMLfile and filename
                XmlDoc.Load("C:\AppFile.xml") 'Path to XML file
                For Each IDNode As XmlNode In XmlDoc.SelectNodes("//user")
                    If IDNode.HasChildNodes = True Then
                        txtFirstName.Text = IDNode.ChildNodes(0).InnerText          '<userid></userid>
                        txtFirstName.Text = IDNode.ChildNodes(1).InnerText          '<first></first>
                        txtLastName.Text = IDNode.ChildNodes(2).InnerText          '<last></last>
                        txtEmail.Text = IDNode.ChildNodes(3).InnerText                 '<email></email>
                    End If
                Next
              Catch ex As Exception
                MsgBox(ex.Message)
            End Try
I am still working on how to Edit, Remove an xml node in the example above.

I use alot. It is just somthing that I am use to using.
I like using XML in my projects. Im not as good as using XML as I would like. But everytime I work with it in VB I do learn something new.
Image
9 posts Page 1 of 1
Return to “Tutorial Requests”