-1

I will give a set of simple xml lines I found in w3schools XML example.

I want to know how I will be able to access these data from my Console application (I inserted a app.config into by console application project)

<?xml version="1.0" encoding="ISO-8859-1"?>
 <bookstore>
   <book category="cooking">
     <title lang="en">Everyday Italian</title>
     <author>Giada De Laurentiis</author>
     <year>2005</year>
     <price>30.00</price>
   </book>
   <book category="children">
     <title lang="en">Harry Potter</title>
     <author>J K. Rowling</author>
     <year>2005</year>
     <price>29.99</price>
   </book>
 </bookstore>
2
  • 2
    Are you asking how to read a generic XML file, or how to read the app.config file, because those two operations are different. Also, what have you tried so far? Dec 11, 2012 at 5:09
  • Actually i wanted to access xml data... When i accidently inserted a application configuration file i found out it also has the starting like <?xml version="1.0" encoding="utf-8" ?> . So i thought i could implement my xml data into application config.. Sorry i might have confused you by the question.. Even i was confused and frustrated by not getting what i wanted....
    – Ashwin A
    Dec 11, 2012 at 5:35

2 Answers 2

6

I'll assume that you just want to read the XML data. It doesn't really look like configuration so I suppose you're better off putting this in a regular XML file.

You could parse the XML like this:

var doc = new XmlDocument();
doc.Load(@"XmlFile.xml");

var root = doc.DocumentElement;
if (root == null)
    return;

var books = root.SelectNodes("book");
if(books == null)
    return;

foreach (XmlNode book in books)
{
    var title = book.SelectSingleNode("title");
    var auth = book.SelectSingleNode("author");
    //And so on
}
3
  • Thanks so much for the effort :) !!! I was confused.. And didnt know how to access xml data..
    – Ashwin A
    Dec 11, 2012 at 5:36
  • country.SelectSingleNode? where is country coming from?
    – Cybercop
    Oct 22, 2013 at 8:01
  • No idea :) Fixed it, thanks
    – johan
    Oct 23, 2013 at 5:15
2

if you want to access App.Config you can use ConfigurationManager like this :

string filetype = ConfigurationSettings.AppSettings [ "filetype" ] ;

and if you want to work with XML file in c# you can use this link or you can use LINQ to XML

Not the answer you're looking for? Browse other questions tagged or ask your own question.