Solved

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

Posted on 2013-06-25
10
325 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
[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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
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

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!

Question has a verified solution.

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

Suggested Solutions

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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