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

x
?
Solved

Read data from XML string

Posted on 2009-02-18
6
Medium Priority
?
943 Views
Last Modified: 2013-12-17
Hi,
How would i read the the following XML string. I would like to read the values, and then add them to an array list. So the array list should have 2 records with its 3 values.

Thanks
<AccessoryList>
	<Accessory>
		<Category>Radio</Category>
		<Description>Noise1</Description>
		<Price>50</Price>
	</Accessory>
	<Accessory>
		<Category>Radio2</Category>
		<Description>Noise2</Description>
		<Price>60</Price>
	</Accessory>
</AccessoryList>

Open in new window

0
Comment
Question by:DeonM
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Ken Fayal
ID: 23668417
Here is something you can try.
DataSet ds = new DataSet();
 
XmlReader xmlRdr = XmlReader.Create((HttpContext.Current.Server.MapPath("[Your path to the xml file]")));
ds.ReadXml(xmlRdr);
xmlRdr.Close();

Open in new window

0
 
LVL 16

Expert Comment

by:CuteBug
ID: 23669378
Use LinQ
using System.Xml.Linq;
using System.Collections;
 
namespace ConsoleApplication1
{
    public class Record
    {
        public string Category { get; set; }
        public string Description { get; set; }
        public string Price { get; set; }
    }
 
    class Program
    {
        static ArrayList Records = new ArrayList();
 
        static void Main(string[] args)
        {
            XDocument xDoc = XDocument.Load(@"<your_xml_file_path>");
            XElement xElem = xDoc.Element("AccessoryList");
 
            if (xElem != null)
            {
                foreach (XElement elem in xElem.Elements("Accessory"))
                {
                    Record r = new Record();
 
                    r.Category = elem.Element("Category").Value;
                    r.Description = elem.Element("Description").Value;
                    r.Price = elem.Element("Price").Value;
 
                    Records.Add(r);
                }
            }
        }
    }
}

Open in new window

0
 

Author Comment

by:DeonM
ID: 23670204
Is the above example possible to change without using LinQ?
Cant find the Xdocument and Xelement classes.

Thanks
0
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.

 
LVL 16

Expert Comment

by:CuteBug
ID: 23673002
XDocument and XElement classes are defined in System.Xml.Linq namespace.

You need to install .Net 3.5 Framework for this purpose.
0
 

Author Comment

by:DeonM
ID: 23678626
My project is set to framework 2.0 as not all of our clients have framework 3.5. SO unfortunately i cannot use LinQ.
0
 
LVL 16

Accepted Solution

by:
CuteBug earned 2000 total points
ID: 23680110
Ok. Then use the following code

using System.Collections;
using System.Xml;
 
namespace ConsoleApplication1
{
    public class Record
    {
        public string Category { get; set; }
        public string Description { get; set; }
        public string Price { get; set; }
    }
 
    class Program
    {
        static ArrayList Records = new ArrayList();
 
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"<your_xml_file_path>");
 
            XmlNodeList nodes = xmlDoc.SelectNodes("AccessoryList/Accessory");
 
            foreach (XmlNode node in nodes)
            {
                Record r = new Record();
 
                r.Category = node.SelectSingleNode("Category").InnerText;
                r.Description = node.SelectSingleNode("Description").InnerText;
                r.Price = node.SelectSingleNode("Price").InnerText;
 
                Records.Add(r);
            }
        }
    }
}

Open in new window

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

571 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