Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

XML File in VB.NET

Posted on 2005-03-07
10
Medium Priority
?
277 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

577 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