Solved

Problems reading an XML file and displaying value in textboxes

Posted on 2013-05-27
5
294 Views
Last Modified: 2013-05-29
Good afternoon,

I'm using Visual Studio Express 2012 for my VB.Net project.

I am trying to set up a settings area for my windows form. Writing to the XML file is not a problem as I can do that sucessfully without issue with my code so far.

The problem comes with read the xml file. Essentially I would like when the user opens the settings form the values from the xml file are inserted to "specific" text boxes on the settings form.

Is anyone able to tell me what I am doing wrong and if possible provide some sample code to help me get the reading part working?

Code is below, i would like to add all id's (1 through 4) to be read in the settings box, just provided id1 for now until i can get the right code for the others.

Public Class SettingsFrm

    Private Sub SettingsFrm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim rdxmlfile As String = "\\ServerA\ITDept\test\Settings.xml"

        'Create the xml xmlreader
        Dim XMLR As New Xml.XmlTextReader(rdxmlfile)

        'Read the id1 element
        XMLR.ReadStartElement("id1")

        'Read the value of the code1 element and display in the appropriate textbox
        XMLR.ReadStartElement("code1")
        XMLR.ReadString(CDtxtbx1.Text)
        XMLR.ReadEndElement()

        'Read the value of the docgroup1 element and display in the appropriate textbox
        XMLR.ReadStartElement("docgroup1")
        XMLR.ReadString(DGtxtbx1.Text)
        XMLR.ReadEndElement()

        'Read the value of the doctype1 element and display in the appropriate textbox
        XMLR.ReadStartElement("doctype1")
        XMLR.ReadString(DTtxtbx1.Text)
        XMLR.ReadEndElement()

        'Read the value of the docsubtype1 element and display in the appropriate textbox
        XMLR.ReadStartElement("docsubtype1")
        XMLR.ReadString(DSTtxtbx1.Text)
        XMLR.ReadEndElement()

        'close the id1 element
        XMLR.ReadEndElement()

        'close cml
        XMLR.Close()

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim svxmlfile As String = "\\ServerA\ITDept\test\Settings.xml"

        'Create the xml writer obeject
        Dim XMLW As New Xml.XmlTextWriter(svxmlfile, System.Text.Encoding.GetEncoding("UTF-8"))

        'xml format to be written as:
        '<id1>
        '   <code1> FTO </code1>
        '   <docgroup1> Operations </docgroup1>
        '   <doctype1> Funds Transfer </doctype1>
        '   <docsubtype1> Out </docsubtype1>
        '</id1>

        XMLW.Formatting = Xml.Formatting.Indented

        'write id1
        XMLW.WriteStartElement("id1")

        ' <code element>
        XMLW.WriteStartElement("code1")
        XMLW.WriteValue(CDtxtbx1.Text)
        XMLW.WriteEndElement()

        ' <DocGroup element>
        XMLW.WriteStartElement("docgroup1")
        XMLW.WriteValue(DGtxtbx1.Text)
        XMLW.WriteEndElement()

        ' <DocType element>
        XMLW.WriteStartElement("doctype1")
        XMLW.WriteValue(DTtxtbx1.Text)
        XMLW.WriteEndElement()

        ' <DocSubType element>
        XMLW.WriteStartElement("docsubtype1")
        XMLW.WriteValue(DSTtxtbx1.Text)
        XMLW.WriteEndElement()

        'the end of id1
        XMLW.WriteEndElement()

        'write id2
        XMLW.WriteStartElement("id2")

        ' <code element>
        XMLW.WriteStartElement("code2")
        XMLW.WriteValue(CDtxtbx2.Text)
        XMLW.WriteEndElement()

        ' <DocGroup element>
        XMLW.WriteStartElement("docgroup2")
        XMLW.WriteValue(DGtxtbx2.Text)
        XMLW.WriteEndElement()

        ' <DocType element>
        XMLW.WriteStartElement("doctype2")
        XMLW.WriteValue(DTtxtbx2.Text)
        XMLW.WriteEndElement()

        ' <DocSubType element>
        XMLW.WriteStartElement("docsubtype2")
        XMLW.WriteValue(DSTtxtbx2.Text)
        XMLW.WriteEndElement()

        'the end of id2
        XMLW.WriteEndElement()

        'write id3
        XMLW.WriteStartElement("id3")

        ' <code element>
        XMLW.WriteStartElement("code3")
        XMLW.WriteValue(CDtxtbx3.Text)
        XMLW.WriteEndElement()

        ' <DocGroup element>
        XMLW.WriteStartElement("docgroup3")
        XMLW.WriteValue(DGtxtbx3.Text)
        XMLW.WriteEndElement()

        ' <DocType element>
        XMLW.WriteStartElement("doctype3")
        XMLW.WriteValue(DTtxtbx3.Text)
        XMLW.WriteEndElement()

        ' <DocSubType element>
        XMLW.WriteStartElement("docsubtype3")
        XMLW.WriteValue(DSTtxtbx3.Text)
        XMLW.WriteEndElement()

        'the end of id3
        XMLW.WriteEndElement()

        'write id4
        XMLW.WriteStartElement("id4")

        ' <code element>
        XMLW.WriteStartElement("code4")
        XMLW.WriteValue(CDtxtbx4.Text)
        XMLW.WriteEndElement()

        ' <DocGroup element>
        XMLW.WriteStartElement("docgroup4")
        XMLW.WriteValue(DGtxtbx4.Text)
        XMLW.WriteEndElement()

        ' <DocType element>
        XMLW.WriteStartElement("doctype4")
        XMLW.WriteValue(DTtxtbx4.Text)
        XMLW.WriteEndElement()

        ' <DocSubType element>
        XMLW.WriteStartElement("docsubtype4")
        XMLW.WriteValue(DSTtxtbx4.Text)
        XMLW.WriteEndElement()

        'the end of id4
        XMLW.WriteEndElement()

        'close the element
        XMLW.Close()

        MessageBox.Show("Settings Saved")
    End Sub

End Class

Open in new window


Much thanks,
N
0
Comment
Question by:nobushi
  • 3
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 39199914
Your xml is very hard to maintain and it is not searchable because all the keys are different, You should change the id nodes to:
        '<info>
        '   <id>1</id>
        '   <code> FTO </code>
        '   <docgroup> Operations </docgroup>
        '   <doctype> Funds Transfer </doctype>
        '   <docsubtype> Out </docsubtype>
        '</info>

Open in new window

Thus, you can easily generate your xml file, then read the values using xpath:
http://www.techrepublic.com/article/easily-navigate-xml-with-vbnet-and-xpath/5035074
http://www.aspfree.com/c/a/net/working-with-xpath-the-net-way/
0
 
LVL 1

Author Comment

by:nobushi
ID: 39202009
What do you mean by not searchable?

I am only using the xml file as a place to store my applications settings. I need each node to be identifiable so that I can use the information within them to do other things within my application.

My form looks like below:

settings form
As i said before i can write no problem to the xml file my only problem now is to read the nodes from the form.

If an xml file is not the way to go with what I want to do what other options are there, ive read on several forums that ini files are outdated so this is why i didnt go this route.

Another Expert said i should use a DGV but i dont want to go this route and want to use text boxes.

I'm a beginner to VB.Net and programming in general, so if you can provide some other ideas and sample code, i would be most appreciative.

Please advise.
N
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39203089
Not searchable = means it is hard to search for a key using best practices (Xpath) because the proporty contains the Id.
My suggestion is the best way to build the xml file as the code only needs to loop every info record without looking for funny property names with id embedded into it.
UI textboxes is OK as how you store should independent of UI.

What is DGV?
0
 
LVL 1

Author Comment

by:nobushi
ID: 39204348
DataGridView

I'll give xpath a shot.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39206218
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System32Int Error 8 56
DataGridview Currency Formating? 22 53
Can Amazon AppStream 2.0 be used for VB.NET and VB6 applications? 2 79
Programmatically signing Word macros 4 61
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now