Solved

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

Posted on 2013-06-25
10
322 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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