[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help With XML

Posted on 2006-03-20
3
Medium Priority
?
476 Views
Last Modified: 2008-02-26
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!
0
Comment
Question by:clickclickbang
3 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 16239628
Try

foreach (XmlNode node in xml.DocumentElement.ChildNodes)
                    {
                        ReadNode(node);
                    }
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16239864
This will grab all of the Project nodes and iterate through its children:


        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(args[0]);

            XmlNodeList nodes = doc.SelectNodes("/backup/Project");

            foreach (XmlNode node in nodes)
            {
                foreach (XmlNode tmp in node.ChildNodes)
                    ReadNode(tmp);
            }

            Console.ReadLine();
        }

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

Author Comment

by:clickclickbang
ID: 16240086
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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