Page 1 of 1

Labels text = xml value?

Posted: Thu Mar 03, 2011 1:22 am
by 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

Re: Labels text = xml value?

Posted: Thu Mar 03, 2011 1:11 pm
by 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

Re: Labels text = xml value?

Posted: Thu Mar 03, 2011 8:59 pm
by zachman61
Mandai, You should write a book on this :D

Re: Labels text = xml value?

Posted: Thu Mar 03, 2011 9:06 pm
by GoodGuy17
You've got me interested now, Zach :P

Hey Mandai, what if there is more than one txt tag?

Re: Labels text = xml value?

Posted: Thu Mar 03, 2011 9:08 pm
by 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

Re: Labels text = xml value?

Posted: Thu Mar 03, 2011 9:38 pm
by 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()

Re: Labels text = xml value?

Posted: Thu Mar 03, 2011 10:08 pm
by 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?

Re: Labels text = xml value?

Posted: Fri Mar 04, 2011 11:46 am
by 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>

Re: Labels text = xml value?

Posted: Fri Mar 04, 2011 4:54 pm
by 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.