Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium



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

Question by:Dodger42
  • 3
  • 2
LVL 12

Accepted Solution

sumix earned 2000 total points
ID: 16287109

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

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


Author Comment

ID: 16291527
Awesome.... worked perfectly.

Author Comment

ID: 16291540
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

ID: 16292540
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

577 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