?
Solved

XML File in VB.NET

Posted on 2005-03-07
10
Medium Priority
?
272 Views
Last Modified: 2013-11-19
I have tried many different ways to write and read to and from a XML file.

But does anybody have a simple way to do the following?

On startup, I type text into a textbox, I want to be able to save this text to a XML file. I also want to be able to select from three radio buttons. and write this selection to the same XML file but under a different XMLnode.

Then, when I re-run the app, the text box is populated and the radio button is selected to what I had previously set it to.

Any ideas?
0
Comment
Question by:cjinsocal581
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13482067
HEre is my Save to XML function:
_____________________________________________
    Private Sub SaveItemsXML()
        Dim xmlSAVE As New XmlTextWriter(fileName, Encoding.ASCII)

        xmlSAVE.Formatting = Formatting.Indented
        xmlSAVE.WriteStartDocument()
        xmlSAVE.WriteComment("Item List Created With XMLTextWriter")
        xmlSAVE.WriteStartElement("items")
       
        Dim line As String
        If RadioButton11.Checked = True Then
            line = "True"
        Else
            line = "False"
        End If
        xmlSAVE.WriteElementString("radiobuttons", line)
        xmlSAVE.WriteElementString("fromlocation", txtPathLoc.Text)
       
        xmlSAVE.WriteEndElement()
        xmlSAVE.Close()
        MsgBox("Settings Saved to XML File")
    End Sub
_______________________________________________________________
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13482074
Oh, and it works the way I need it to.

Now, I just need to read from it, and populate the elements needed.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13482771
Hi, here's a brief set of lines to get you started on how to read from a file:

Firstly... this is some sample xml that im reading from...

<settings>
    <PresetRules>
         <rule name="Rule1">BLAH BLAH</rule>
         <rule name="Rule2">BLAH BLAH and then some</rule>
         <rule name="Rule3">BLAH BLAH and a bit more</rule>
    </PresetRules>
    <OtherRules>
         <rule name="Rule1">BLAH BLAH</rule>
         <rule name="Rule2">BLAH BLAH and then some</rule>
         <rule name="Rule3">BLAH BLAH and a bit more</rule>
    </OtherRules>

</settings>


            Dim xSettings As New XmlDocument()
            Dim nodeRoot, nodePresets, nodeOther, nodeChild as XMLNode
            xSettings.Load("mysettings.xml")

            nodeRoot = xSettings.SelectSingleNode("//settings")
            nodePresets = nodeRoot.SelectSingleNode("//PresetRules")
            nodeOther = nodeRoot.SelectSingleNode("//OtherRules")

            For each nodeChild in nodePresets
                msgbox(nodeChild.Attributes("name").InnerText)
                msgbox(nodeChild.innerXML)
            Next

            For each nodeChild in nodeOther
                msgbox(nodeChild.Attributes("name").InnerText)
                msgbox(nodeChild.innerXML)
            Next

There are obviously plenty of other bits, but this should help
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13482782
If you need help in interpreting the values so it sets the textbox and radio buttons... or anything else, just ask.
0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13482829
           Dim xSettings As New XmlDocument()
            Dim nodeRoot, nodeRadio, nodeFrom as XMLNode
            xSettings.Load(filename)

            nodeRoot = xSettings.SelectSingleNode("//items")
            nodeRadio = nodeRoot.SelectSingleNode("//radiobuttons")
            nodeFrom = nodeRoot.SelectSingleNode("//fromlocation")

            if nodeRadio.innerText = "True" Then
                 RadioButton11.Checked = True
            Else
                 RadioButton11.Checked = False
            End If

            txtPathLoc.Text = nodeFrom.innerText

==============

Now I think that's what would be your "loading".  Just a note... im not sure how the xmlwriter handles writing characters like < >  or other markup characters, whether it escapes them or not, but just take note when writing with certain characters, and when reading them... might find some  differences (although im not sure)
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13483055
Perfect. Thanks!!
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13483077
By the way, how should the XML File look now?
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13483083
The reason I ask is, I am now getting a system error with the XML library.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483087
where are you getting an error?
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13483119
Sorry, there was too many references to one XML file.

Everything works correctly.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

770 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