Link to home
Start Free TrialLog in
Avatar of clickclickbang
clickclickbang

asked on

Help With XML

I have an xml file that I need to read. The format is as follows:

<backup>
  <Project>
    <DirectoryLocation>c:\projects\</DirectoryLocation>
    <ExcludeFolder>\images</ExcludeFolder>
    <Destination>e:\projects\</Destination>
  </Project>
</backup>

I need to write a loop for each project node that gets the values of each of the child nodes. There can be more than one ExcludeFolder.

I'm using the following to iterate through the XML document, but need to modify it to perform some type of loop for each project node.

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                XmlDocument xml = new XmlDocument();

                try
                {
                    xml.Load(args[0]);

                    foreach (XmlNode node in xml.DocumentElement)
                    {
                        ReadNode(node);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error! Configuration File Not Found.");
                    Console.WriteLine(ex.Message);
                    return;
                }
            }
            else
            {
                Console.WriteLine("Error! No Configuration File Specified.");
            }
        }

        static void ReadNode(XmlNode node)
        {
            Console.WriteLine(node.InnerText);
        }

Thanks for help. This is my first time working with XML so if I have started down the wrong path, feel free to point it out!
Avatar of mrichmon
mrichmon

Try

foreach (XmlNode node in xml.DocumentElement.ChildNodes)
                    {
                        ReadNode(node);
                    }
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland 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 clickclickbang

ASKER

Thanks!