how to read through all elements of a XML file in C#

Posted on 2004-11-21
Last Modified: 2011-09-20
XML file:

hi, anyone know some function or class or some code to read all element under a Element(parent) like <A>. This parent can has many child and sub childs, and sub sub childs ........So when user select element A then program can list  a2 .....a5 and more in some sub level.
If user select a3, then the program list sub elements and its childs from a4.......
Thank you
Question by:therock_80
    LVL 6

    Accepted Solution

    The concept is simple, but the most practical approach to coding this depends on your XML structure and what information you need. Anyway,

    using System.Xml;

    XmlDocument d = new XmlDocument();
    d.LoadXml("xml goes here"); // You can also use Load() to load from a file

    XmlNode root = d.DocumentElement;

    Now, root.ChildNodes returns a collection of the immediate children of the root node - they're also XmlNodes with possible ChildNodes again. One approach is to go through ChildNodes recursively, another one involves using the XPath query language. Unless you know XPath, stick with going through the ChildNodes collections.
    LVL 1

    Author Comment

    hi der_jth, can i use XML as a database? though i know it as data format, but if i can be a database, how can i make this xml file security. For example, i dont want any one access this XML file except me.
    LVL 6

    Expert Comment

    The answer to that question isn't particularly simple, but the short version: You can use XML documents as a sort of database - see the XmlDataDocument class and a good book. File security is achieved through normal file system access control lists.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now