Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

How to read a xml file in C#

I need to read an xml file that has the following lines:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSplitBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SplitBase Exchange="NYSE" Symbol="HEI" DateTime="2011-04-26T00:00:00" Ratio="5-4" />
  <SplitBase Exchange="NYSE" Symbol="EEQ" DateTime="2011-04-25T00:00:00" Ratio="2-1" />
  <SplitBase Exchange="NYSE" Symbol="EEP" DateTime="2011-04-25T00:00:00" Ratio="2-1" />
  <SplitBase Exchange="NYSE" Symbol="SF" DateTime="2011-04-06T00:00:00" Ratio="3-2" />

I can open the file in C#.

        // Create an instance of XmlTextReader and call Read method to read the file

        XmlTextReader textReader = new XmlTextReader(@pstrSourceFile);

        textReader.Read();

        // If the node has value

        while (textReader.Read())
        {
              Write the SplitBase Exchange value ("NYSE") into string1
            // Move to first element

What is the syntax for reading each element into variables?

                     Write the SplitBase Exchange value ("NYSE") into string1
                     Write the Symbol value("HEI") into string2
                     etc.    

The examples I found refer to books.xml

Where can I find books.xml for downloading?

Thanks,



Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dovberman

ASKER

OK
I need help with the terminology.

<?xml version="1.0" encoding="utf-8"?>  Is this the root node? if so, where is the closing node?
<ArrayOfSplitBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SplitBase Exchange="NYSE" Symbol="HEI" DateTime="2011-04-26T00:00:00" Ratio="5-4" />
  <SplitBase Exchange="NYSE" Symbol="EEQ" DateTime="2011-04-25T00:00:00" Ratio="2-1" />
  <SplitBase Exchange="NYSE" Symbol="EEP" DateTime="2011-04-25T00:00:00" Ratio="2-1" />
  <SplitBase Exchange="NYSE" Symbol="SF" DateTime="2011-04-06T00:00:00" Ratio="3-2" />
</ArrayOfSplitBase>

I guess that I need to move to each line.

SplitBase Exchange="NYSE" Symbol="EEP" DateTime="2011-04-25T00:00:00" Ratio="2-1"

Is the content of a line
If this is true, then I need to parse the line for
   "SplitBase Exchange=" and read the text that follows
   "Symbol=" and read the text that follows
Hopefully, you can help me with syntax for this specific file.

Thanks,

   
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or a bit modified. It's not much different from mrjoltcola's but bit more clear...
private void button1_Click(object sender, EventArgs e)
        {
            //Read xml file to the object
            XmlDocument doc = new XmlDocument();
            doc.Load("XMLFile1.xml");

           // we aren't really interested in the root element here. Look for content elements
           XmlNodeList nodes =  doc.SelectNodes("//SplitBase");
            // we have 4 elements in the example and we may check their content:
            int counter = 0;
           foreach (XmlNode node in nodes)
           {
               counter++;
               // print our each element with it's attributes and their values: 
               XmlElement ele = (XmlElement)node;
               XmlAttribute at1 = node.Attributes["Exchange"];
               XmlAttribute at2 = node.Attributes["Symbol"];
               XmlAttribute at3 = node.Attributes["DateTime"];
               XmlAttribute at4 = node.Attributes["Ratio"];
               System.Diagnostics.Debug.WriteLine(node.Name + counter+ ":  " + Environment.NewLine + 
                   "\t" + at1.Name + "=" + at1.Value +Environment.NewLine + 
                   "\t" + at1.Name + "=" + at2.Value +Environment.NewLine + 
                   "\t" + at1.Name + "=" + at3.Value +Environment.NewLine + 
                   "\t" + at1.Name + "=" + at4.Value +Environment.NewLine );
                                                                                        

           }

Open in new window

anarki_jimbel:
 I do not want to writeline.

I need to set the line content into a variable.

string strExchange=" ";
string strSymbol = " ";
foreach (XmlNode node in nodes)
           {
               counter++;
               // print our each element with it's attributes and their values:
               XmlElement ele = (XmlElement)node;
               XmlAttribute at1 = node.Attributes["Exchange"];
               XmlAttribute at2 = node.Attributes["Symbol"];
               XmlAttribute at3 = node.Attributes["DateTime"];
               XmlAttribute at4 = node.Attributes["Ratio"];
              // Set the Exchange value into strExchange

strExchange = ?
Should return "NYSE"
strSymbol = ?
Should return "HEI"

Why is it so complicated to simply read the content of a line into a string ? If I can do this, I can parse the string for the values of Exchange, Symbol, etc.

ie
strLineContent = ?
strLineContent = SplitBase Exchange="NYSE" Symbol="HEI" DateTime="2011-04-26T00:00:00" Ratio="5-4"

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oxyoo:

This makes sense.

I will try it tomorrow.

Thanks,
I found a tab delimited text file. I no longer need to read an xml file.

Thanks fo all your help.