Posted on 2006-03-24
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>

Question by:Dodger42
    LVL 12

    Accepted Solution


    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]");


    Author Comment

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


    Author Comment

    Awesome.... worked perfectly.

    Author Comment

    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?

    LVL 12

    Expert Comment

    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


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 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

    19 Experts available now in Live!

    Get 1:1 Help Now