xmlreader

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>

Dodger42Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sumixCommented:

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dodger42Author Commented:
Great, that looks perfect. Will give it a shot now.

Thanks.
0
Dodger42Author Commented:
Awesome.... worked perfectly.
0
Dodger42Author 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.
0
sumixCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.