We help IT Professionals succeed at work.


Dodger42 asked
Medium Priority
Last Modified: 2010-04-16
I want to process data in an XML file.

I want to grab the global <settings> values below into strings and an int for the percent. But then, based on the value of int currentIndex, then grab the details of the appropriate mission.

                    xtr = new XmlTextReader(openFileDialog.FileName);

How though would I grab the subelement name? If I try to String strMission1Name = xtr.ReadElementString("name"); I get an error because name is not the next element (I presume).
Additionally, once I grab those global <settings>, how can I then move on to process a mission based on its index number? Should I wrap the missions in an element called <missions> and then just use the index number for each mission?

<?xml version="1.0" encoding="iso-8859-1"?>
            <description>The first attempt at a campaign</campaign>

Watch Question


You could use an XmlDocument object and select nodes in your document using XPath expressions.

Load the document:
    XmlDocument xd = new XmlDocument();

retrieve the <settings> node and write the value of name child:

   XmlNode xnSettings = xd.DocumentElement.SelectSingleNode("settings");

retrieve node <mission> that has a child <index> element with value 2 and read its <mtitle> value:
    XmlNode xMis = xd.DocumentElement.SelectSingleNode("mission[index=2]");

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Great, that looks perfect. Will give it a shot now.



Awesome.... worked perfectly.


By the way, if I wanted to use c# to count the number of missions in the xml file, any idea how i could do that?


XmlNode class also has a SelectNodes method which returns a XmlNodeList containing all the nodes that meet the specified criteria:

  XmlNodeList xl = xd.DocumentElement.SelectNodes("mission");
  Console.WriteLine(xl.Count.ToString());  // the number of <mission> nodes

 You can access a node from this XmlNodeList with

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.