Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-06-25
10
Medium Priority
?
338 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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