locked
reading/writing xml file's and tags RRS feed

  • Question

  • Hi guys.

    im "new" to VB

    i need to make a program
    that can read and write xml tags..

    it will need to write to the following tags

    <name></name>
    <age></age>
    <job></job>

    it should be able to both read whatever is inside name,
    or create a whole new doc with user typed name,age,job. ?

    can anyone please help me out here? Smile
    Tuesday, August 14, 2007 3:36 PM

Answers

  • Hi,

     

    Based on your post, you want to read and write the xml files. I think you may use the XmlReader and XmlWriter class to work with the XML document in the .NET Framework. First it is important to know about the System.Xml namespace. This namespace contains many classes to read and write the XML documents. Here is the simple code example to use the XmlReader and XmlWriter class to read and write xml files. Hope this helps.  

    Code Snippet

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim setting As XmlWriterSettings = New XmlWriterSettings()

            setting.Indent = True

            setting.NewLineOnAttributes = True

            Dim xw As XmlWriter = XmlWriter.Create("c:\booknew.xml", setting)

            xw.WriteStartDocument()

            xw.WriteStartElement("person")

            xw.WriteStartElement("person1")

            xw.WriteElementString("Name", " Diego")

            xw.WriteElementString("Age", "29")

            xw.WriteElementString("Job", "worker")

            xw.WriteEndElement()

            xw.WriteStartElement("person2")

            xw.WriteElementString("Name", "John")

            xw.WriteElementString("Age", "24")

            xw.WriteElementString("Job", "engineer")

            xw.WriteEndElement()

            xw.WriteEndDocument()

            xw.Flush()

            xw.Close()

        End Sub

     

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Me.RichTextBox1.Clear()

            Dim xr As XmlReader = XmlReader.Create("c:\booknew.xml")

            xr.Read()

            Dim flag As Boolean = False

            While (xr.Read())

                If (xr.NodeType = XmlNodeType.Element) Then

                    flag = True

                    Debug.Print(xr.Name)

                End If

                If (xr.NodeType = XmlNodeType.Text And flag = True) Then

                    flag = False

                    Debug.Print(xr.Value.ToString)

                End If

            End While

        End Sub

     

    Here are one good article about how to read and write xml files. Please visit here:

    http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx

     

     

    Thanks for your questions.

     

    Monday, August 20, 2007 1:05 PM
    Moderator
  • Hi M-Studiois,

    I tested the exact code provided, and came up with the following using two textboxes, one for name and one for age..
      

    Code Snippet
    Dim xmlDoc As New XmlDocument


                    xmlDoc.Load("c:\booknew.xml")

                    Dim nodeList As XmlNodeList = xmlDoc.SelectNodes("/person/person1/Name")
                    Dim nodeList2 As XmlNodeList = xmlDoc.SelectNodes("/person/person1/Age")

                    For Each nod As XmlNode In nodeList
                        TextBox1.Text = nod.InnerText

                    Next


                    For Each nod As XmlNode In nodeList2
                        TextBox2.Text = nod.InnerText
                    Next

     

     




    hope this helps Smile

     

    Tuesday, August 21, 2007 12:10 AM

All replies

  • Hi,

     

    Based on your post, you want to read and write the xml files. I think you may use the XmlReader and XmlWriter class to work with the XML document in the .NET Framework. First it is important to know about the System.Xml namespace. This namespace contains many classes to read and write the XML documents. Here is the simple code example to use the XmlReader and XmlWriter class to read and write xml files. Hope this helps.  

    Code Snippet

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim setting As XmlWriterSettings = New XmlWriterSettings()

            setting.Indent = True

            setting.NewLineOnAttributes = True

            Dim xw As XmlWriter = XmlWriter.Create("c:\booknew.xml", setting)

            xw.WriteStartDocument()

            xw.WriteStartElement("person")

            xw.WriteStartElement("person1")

            xw.WriteElementString("Name", " Diego")

            xw.WriteElementString("Age", "29")

            xw.WriteElementString("Job", "worker")

            xw.WriteEndElement()

            xw.WriteStartElement("person2")

            xw.WriteElementString("Name", "John")

            xw.WriteElementString("Age", "24")

            xw.WriteElementString("Job", "engineer")

            xw.WriteEndElement()

            xw.WriteEndDocument()

            xw.Flush()

            xw.Close()

        End Sub

     

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Me.RichTextBox1.Clear()

            Dim xr As XmlReader = XmlReader.Create("c:\booknew.xml")

            xr.Read()

            Dim flag As Boolean = False

            While (xr.Read())

                If (xr.NodeType = XmlNodeType.Element) Then

                    flag = True

                    Debug.Print(xr.Name)

                End If

                If (xr.NodeType = XmlNodeType.Text And flag = True) Then

                    flag = False

                    Debug.Print(xr.Value.ToString)

                End If

            End While

        End Sub

     

    Here are one good article about how to read and write xml files. Please visit here:

    http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx

     

     

    Thanks for your questions.

     

    Monday, August 20, 2007 1:05 PM
    Moderator
  • Hey!

    the writing is GREAT! Big Smile thanks a lot.

    the reading is okey, i can se it writes it in the debugging output.
    but how do i use it. forexample so i can put the name in one textbox, and the age in another textbox? (:

    please help me further.!
    Monday, August 20, 2007 2:18 PM
  • Hi M-Studiois,

    I tested the exact code provided, and came up with the following using two textboxes, one for name and one for age..
      

    Code Snippet
    Dim xmlDoc As New XmlDocument


                    xmlDoc.Load("c:\booknew.xml")

                    Dim nodeList As XmlNodeList = xmlDoc.SelectNodes("/person/person1/Name")
                    Dim nodeList2 As XmlNodeList = xmlDoc.SelectNodes("/person/person1/Age")

                    For Each nod As XmlNode In nodeList
                        TextBox1.Text = nod.InnerText

                    Next


                    For Each nod As XmlNode In nodeList2
                        TextBox2.Text = nod.InnerText
                    Next

     

     




    hope this helps Smile

     

    Tuesday, August 21, 2007 12:10 AM
  • That did the trick!

    NICE! .D

    thanks.. Smile

    im going to apply it to an application for sorting my homeworlk,, quite nice actually.. ^^

    thanks a million! Big Smile
    Tuesday, August 21, 2007 6:28 PM
  • No problem at all M-Studios Smile , if you have anymore questions, please feel free to ask.
    Tuesday, August 21, 2007 7:08 PM
  • well

    do you know a way so that .net reflectors can't see the code? Wink
    Tuesday, August 21, 2007 7:25 PM
  • If I remember correctly, I read before somewhere, users had mentioned about using a packing or compression program on the files to keep it from being able to decompile them.
    Tuesday, August 21, 2007 7:38 PM
  • did you ever get the name? (:
    Wednesday, August 22, 2007 5:17 AM
  • Hi,

    After doing a bit of searching here, I found one that Microsoft reccomends, called "Dotfuscator"

    "PreEmptive Solutions’ Dotfuscator is the leading .NET Obfuscator, Compactor and Watermarker that helps protect programs against reverse engineering while making them smaller and more efficient. Dotfuscator Professional Edition is designed to stop even the best of decompilers from producing useful output. It provides comprehensive and efficient .NET code development and deployment."

    Microsofts talk about it
    http://msdn2.microsoft.com/en-us/library/ms227240(VS.80).aspx


    MSDN Magazine talk about "Thwart Reverse Engineering of your Visual Basic.NET code"
    http://msdn.microsoft.com/msdnmag/issues/03/11/NETCodeObfuscation/


    Dotfuscator main site
    http://www.preemptive.com/products/dotfuscator/


    hope this helps Smile



    Wednesday, August 22, 2007 11:57 AM
  • OK people,

    I have had a similar issue but would like to place the value of an attribute into a text box.  Keo1, your code seems to be the closest to what I want as it specifies the element the is to be read, but what if you need to refer to an attribute?

    This is my first time using these forums and I hope you can help.

    Thanks in advance

    John1million

    Thursday, September 6, 2007 12:42 PM
  • Hey.. Smile

    i havn't got a copy og Visual studio right here at hand. since im in school.
    but i think i remebered something caled noteatribute? intead of node, or element attribute?
    i can't remember but look afte rit, might help you Wink

    hope it helps!

    REGARDS:
    Martin Jørgensen.
    Thursday, September 6, 2007 3:24 PM