Solved

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

Posted on 2013-06-25
10
321 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
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.

 
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

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

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

12 Experts available now in Live!

Get 1:1 Help Now