Solved

Reading XML Node values (vb.net)

Posted on 2011-02-23
4
409 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
Comment Utility
By keys do you mean the attributes?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Great thank you

Andy
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

10 Experts available now in Live!

Get 1:1 Help Now