Login  |  Register
Home | Articles | Downloads | Resources | About
Contact Us | My Resume
   Topics:  ASP.NET  C#  .NET  XML 
XML
Read XML node attributes data

Posted by on Thursday, March 25, 2004 (PST)

This article will show you how to read XML node attribute data from XML file.

using System;
using System.Xml;
class test
{
    static void Main()
    {
        // Load the XML from file
        XmlTextReader reader = new XmlTextReader(@"data.xml");
        // ignore the DTD
        reader.XmlResolver = null; 
        // ignore whitespace in the  XML file
        reader.WhitespaceHandling = WhitespaceHandling.None;
        // Start from the item Element
    
        // check for content node
        reader.MoveToContent();
        if (reader.Name != null) 
        {
            while (reader.Read()) 
            {
                if(reader.NodeType == XmlNodeType.Element) 
                { 
                    // check if the element has any attributes
                    if (reader.HasAttributes)
                    {
                        // move to the first attribute
                        reader.MoveToFirstAttribute();
                        for(int i = 0; i < reader.AttributeCount; i++) 
                        { 
                            // read the current attribute
                            reader.MoveToAttribute(i);
                            switch (reader.Name) 
                            {
                                case "Name":
                                    Console.Write(" Name : " + reader.Value);
                                    break;
                                case "Capital":
                                    Console.Write(", Capital : " + reader.Value);
                                    break;
                                case "Language":
                                    Console.Write(", Language : " + reader.Value);
                                    break;
                                case "Currency":
                                    Console.WriteLine(", Currency : " + reader.Value);
                                    break;
                                default: // ignore
                                    break;
                            }
                        } 
                        // move back to the element node that contains
                        // the attributes we just traversed
                        reader.MoveToElement();
                    }
                }
            }
        }
    }
}

Xml file data structure:


<?xml version="1.0" encoding="utf-8"?>
<Countries>
    <Country Name="USA" Capital="Washinton DC" Language="English" Currency="Dollars"></Country>
    <Country Name="England" Capital="London" Language="English" Currency="Pounds"></Country>
    <Country Name="France" Capital="Paris" Language="French" Currency="Euro"></Country>
    <Country Name="Germany" Capital="Berlin" Language="German" Currency="Mark"></Country>
    <Country Name="Russia" Capital="Moscow" Language="Russian" Currency=""></Country>
    <Country Name="Spain" Capital="Madrid" Language="Spanish" Currency=""></Country>
    <Country Name="Turkey" Capital="Ankara" Language="" Currency=""></Country>
    <Country Name="Norway" Capital="Oslo" Language="" Currency=""></Country>
    <Country Name="Canada" Capital="Ottawa" Language="English" Currency="Dollars"></Country>
    <Country Name="Mexico" Capital="Mexico" Language="Spanish" Currency="Peso"></Country>
    <Country Name="China" Capital="Bejing" Language="Chinnes" Currency=""></Country>
    <Country Name="Japan" Capital="Tokyo" Language="" Currency="Yen"></Country>
    <Country Name="India" Capital="New Delhi" Language="Hindi" Currency="Ruppies"></Country>
    <Country Name="Australia" Capital="Canberra" Language="English" Currency="Dollars"></Country>
    <Country Name="Pakistan" Capital="Islamabad" Language="Urdu" Currency="Ruppies"></Country>
    <Country Name="New Zealand" Capital="Wellingtion" Language="English" Currency="Dollars"></Country>
    <Country Name="Brazil" Capital="Brasilia" Language="" Currency=""></Country>
    <Country Name="Croatia" Capital="Zegreb" Language="Croatian" Currency="Kuna"></Country>
    <Country Name="Cambodia" Capital="Phnom Penh" Language="" Currency=""></Country>
    <Country Name="Kuwait" Capital="Kuwait" Language="Arabic" Currency="Dinnars"></Country>
</Countries>

Add Your Comment

Support this site
If you found all the downloads from Artisticode.com site to be useful, and you feel that it has helped you out, then I'd really appreciate it if you could show your support by donating to Artisticode.com for hosting cost and for the time that I have invested by producing this quality free code, via PayPal.

You can donate as little, or as much as you want by clicking the Make a Donation button. It's free, fast, easy and secure! Your donation will be appreciated.

Thanks for your support!

 
 
Home Articles Downloads Resources About