Solved

VB.Net - How can I display my xml values to a textbox?

Posted on 2013-06-25
10
320 Views
Last Modified: 2016-11-23
Afternoon,

Can someone tell me what would be the code in VB.Net to display my <category> values to 4 textboxes (txtbox1, txtbox2, txtbox3, txtbox4)?

My xml file is below:

<Settings>
  <ApplicationSettings>
    <category id="Servers">
      <Brand>Dell</Brand>
      <type>New</type>
      <department>Accounting</department>
    </category>
    <category id="Printers">
      <Brand>Canon</Brand>
      <type>New</type>
      <department>Marketing</department>
    </category>
    <category id="Desktops">
      <Brand>Sony</Brand>
      <type>Used</type>
      <department>IT</department>
    </category>
    <category id="Plotters">
      <Brand>Signa</Brand>
      <type>Used</type>
      <department>Sales</department>
    </category>
  </ApplicationSettings>
</Settings>

Thanks,
N
0
Comment
Question by:nobushi
  • 5
  • 5
10 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39275966
i assumed u mean category id:
        Dim categories = XElement.Load("c:\temp\1.xml").Element("ApplicationSettings").Elements("category").Select(Function(e) e.Attribute("id").Value).ToArray

        txtbox1.Text = categories(0)
        txtbox2.Text = categories(1)
        txtbox3.Text = categories(2)
        txtbox4.Text = categories(3)

Open in new window

0
 
LVL 1

Author Comment

by:nobushi
ID: 39276005
Yes that's what i meant.

When i try this code you wrote im getting the error:

Lambda parameter 'e' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression.

Any ideas as to what this means?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39276011
        Dim categories = XElement.Load("c:\temp\1.xml").Element("ApplicationSettings").Elements("category").Select(Function(cat) cat.Attribute("id").Value).ToArray

        txtbox1.Text = categories(0)
        txtbox2.Text = categories(1)
        txtbox3.Text = categories(2)
        txtbox4.Text = categories(3)

Open in new window

0
 
LVL 1

Author Comment

by:nobushi
ID: 39276024
Recieving this error now:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in MyProgram.exe
Additional information: Index was outside the bounds of the array.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39276033
can u post the exact xml that u reading in the code?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:nobushi
ID: 39276056
Sedgwick, Please see attached.
settings.xml
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39276065
which line throw the error?
0
 
LVL 1

Author Comment

by:nobushi
ID: 39276071
this line:

txtbox1.Text = categories(0)
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39276097
this error means that parsing the xml yields no category id, which is wierd cause i used the exact same code with the xml u posted and it works fine.
can u make suer the code is as posted and the xml is the one u posted?

        Dim categories = XElement.Load("c:\temp\1.xml").Element("ApplicationSettings").Elements("category").Select(Function(cat) cat.Attribute("id").Value).ToArray
if categories.Length > 0 then
        txtbox1.Text = categories(0)
        txtbox2.Text = categories(1)
        txtbox3.Text = categories(2)
        txtbox4.Text = categories(3)
end if

Open in new window

0
 
LVL 1

Author Comment

by:nobushi
ID: 39276146
Thanks Sedgwick.

This new code worked fine.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

22 Experts available now in Live!

Get 1:1 Help Now