Solved

Reading XML Node values (vb.net)

Posted on 2011-02-23
4
423 Views
Last Modified: 2012-05-11
This is a sample of my config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <info>
        
    <Import timeOfDay="07:30" fileTypes="txt" sourceFolder="C:\Pending" destinationFolder="C:\Processed" logFilePath="log.txt" />
 
  </info>

</configuration>

There are lots of examples on how to get data from an XML file on the web, but my requirement is slightly different.

The key contains several values, how do I get each of them into local variables in my app.

If I could use the ConfigurationManager that would be great, but for now I'm resigned to just getting the data out.

Andy
0
Comment
Question by:Andy Green
[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
  • 2
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 34965241
By keys do you mean the attributes?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34965255
You mean you want to get the attributes of the "Import" node?

Try something like:
        Dim doc As New XmlDocument()
        doc.Load("Your xml file")

        Dim node As XmlNode = doc.SelectSingleNode("configuration/info/Import")

        Dim timeOfDay As String = node.Attributes("timeOfDay").InnerText
        Dim fileTypes As String = node.Attributes("fileTypes").InnerText

        '... etc, etc ....

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34965400
indeed you can use this too

        Dim StockDoc As XDocument = XDocument.Load("FullPathToYourFile.xml")
        Dim xtimeOfDay = From kp In StockDoc.Root.Descendants("Import") Select kp.@<timeOfDay>
        Dim xfileTypes = From kp In StockDoc.Root.Descendants Select kp.@<fileTypes>
        Dim xsourceFolder = From kp In StockDoc.Root.Descendants Select kp.@<sourceFolder>
        Dim xdestinationFolder = From kp In StockDoc.Root.Descendants Select kp.@<destinationFolder>
        Dim xlogFilePath = From kp In StockDoc.Root.Descendants Select kp.@<logFilePath>

Open in new window


The variables (lists) will include ALL attributes from ALL
Import

Open in new window

XElements sequentially.
0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 34965501
Great thank you

Andy
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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