Solved

Reading XML Node values (vb.net)

Posted on 2011-02-23
4
412 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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 …

772 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