Hi i'm trying to read in an XML File that is like the following

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSYMBOL xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SYMBOL Code="AA.P" Name="Alcoa Inc Pf 3.75" />
  <SYMBOL Code="AADR" Name="Wcm Bny Focused Growth Adr ETF" />
  <SYMBOL Code="AAU" Name="Almaden Minerals" />
  <SYMBOL Code="AAVX" Name="Etracs Daily Short 1 Month S&amp;P" />
</ArrayOfSYMBOL>

And Im trying to get a message box to show the Code Values one by one

XmlDocument xml = new XmlDocument();
                xml.LoadXml(filePath);

                XmlNodeList xnList = xml.SelectNodes("SYMBOL");
                foreach (XmlNode xn in xnList)
                {
                    MessageBox.Show(xn["Code"].InnerText);
                }

But I keep get the error Data at the root level is invalid. Line 1,postion 1.
What should I do to get it to work

Cheers Sam

When I use XML, I use the System.Xml.Linq namespace. It seems to be easier.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace DW_393489
{
   class Program
   {
      static void Main(string[] args)
      {
         XDocument doc = XDocument.Load("c:/science/DaniWeb/DW_393489/XMLFile1.xml");
         List<XElement> lstElements = doc.Descendants("SYMBOL").ToList();
         lstElements.ForEach(xe => Console.WriteLine(xe.Attribute("Code").Value));
      }
   }
}

When I use XML, I use the System.Xml.Linq namespace. It seems to be easier.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace DW_393489
{
   class Program
   {
      static void Main(string[] args)
      {
         XDocument doc = XDocument.Load("c:/science/DaniWeb/DW_393489/XMLFile1.xml");
         List<XElement> lstElements = doc.Descendants("SYMBOL").ToList();
         lstElements.ForEach(xe => Console.WriteLine(xe.Attribute("Code").Value));
      }
   }
}

Thanks.

How would I store the data of Code and Name in a 2d Array with the data from the XML file?

...before I answer that:

static void OldWay()
      {
         XmlDocument doc = new XmlDocument();
         doc.Load("c:/science/DaniWeb/DW_393489/XMLFile1.xml");
         XmlNodeList lstXmlNodes = doc.ChildNodes[1].SelectNodes("SYMBOL");

         foreach (XmlNode xn in lstXmlNodes)
         {
            Console.WriteLine(xn.Attributes[0].Value);
         }
      }

If you are sure the "Code" values are unique, I would put them in a dictionary.
The dictionary acts like a list of 2 dimensional arrays with a key and a value.

static void NewWay()
      {
         XDocument doc = XDocument.Load("c:/science/DaniWeb/DW_393489/XMLFile1.xml");
         List<XElement> lstElements = doc.Descendants("SYMBOL").ToList();
         Dictionary<string, string> map_s2sCode2Names = new Dictionary<string,string>();
         lstElements.ForEach(xe => map_s2sCode2Names.Add(xe.Attribute("Code").Value, xe.Attribute("Name").Value));
      }
commented: Very Infomative +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.