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

x
?
Solved

xmlreader

Posted on 2006-03-24
5
Medium Priority
?
350 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>

0
Comment
Question by:Dodger42
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
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();
    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
 

Author Comment

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

Thanks.
0
 

Author Comment

by:Dodger42
ID: 16291527
Awesome.... worked perfectly.
0
 

Author Comment

by:Dodger42
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?


Thanks.
0
 
LVL 12

Expert Comment

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

 Regards,
 sumix
0

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