We help IT Professionals succeed at work.

xmlreader

Dodger42
Dodger42 asked
on
Medium Priority
364 Views
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);
                    xtr.ReadStartElement("campaign");

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"?>
<campaign>
      <settings>
            <name>campaign1</name>
            <author>Mr.Test</author>
            <description>The first attempt at a campaign</campaign>
            <percent>60</percent>
            <path></path>
      </settings>
      
      <mission>
            <index>1</index>
            <mtitle>Battle1</mtitle>
            <fname>mission1.txt</fname>
            <gunits_red>10</gunits_red>
            <gunits_blue>11</gunits_blue>
            <start>true</start>
            <isEnd>false</isEnd>
      </mission>
      
      <mission>
            <index>2</index>
            <mtitle>Battle2</mtitle>
            <fname>mission2.txt</fname>
            <gunits_red>471</gunits_red>
            <gunits_blue>523</gunits_blue>
            <start>false</start>
            <isEnd>true</isEnd>
      </mission>
</campaign>

Comment
Watch Question

Commented:

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

Load the document:
    XmlDocument xd = new XmlDocument();
    xd.Load(openFileDialog.FileName);

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

   XmlNode xnSettings = xd.DocumentElement.SelectSingleNode("settings");
   Console.WriteLine(xnSettings.SelectSingleNode("name").InnerText);

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]");
   Console.WriteLine(xMis.SelectSingleNode("mtitle").InnerText);

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

Ask the Experts

Author

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

Thanks.

Author

Commented:
Awesome.... worked perfectly.

Author

Commented:
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?


Thanks.

Commented:
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
   xl.Item(1)

 Regards,
 sumix
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.

OR

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.