HOW TO: Access XML Data Using DOM in .NET Framework with Visual C# .NET

This article was previously published under Q317664
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual Basic .NET version of this article, see 317663.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Xml
  • System.Text


This step-by-step article explains how to access required data in extensible markup language (XML) documents programmatically by using the System.Xml.XmlDocument class and the related classes.

The System.Xml.XmlDocument class implements the core XML Document Object Model (DOM) parser of the .NET Framework. The System.Xml.XmlDocument class is compliant with the World Wide Web Consortium (W3C) DOM Level 1 and Level 2 Core standards.

XML content can be classified broadly into a collection of nodes and attributes of those nodes. You can use the DOM model implementation of System.Xml to query or to access these nodes and attributes of XML documents. The DOM implmentation of System.Xml provides several ways to access these nodes and the attributes.

back to the top


The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Windows XP, Windows 2000, or Windows NT 4.0 Service Pack 6a
  • Microsoft Data Access Components (MDAC) 2.6 or later
  • Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Visual C# .NET syntax
  • XML and the related standards
back to the top

System.Xml Methods and Properties

This section describes several methods and properties of System.Xml classes. For more information, see the Microsoft Framework .NET Software Development Kit (SDK) documentation.

You can use the SelectNodes and the SelectSingleNode methods that the XmlDocument class implements to specify and to execute XPath queries against XML data that is loaded into the DOM. For additional information about how to use these methods and to see a relevant code sample, click the article number below to view the article in the Microsoft Knowledge Base:
318499 HOW TO: Use the System.Xml.XmlDocument Class to Execute XPath Queries in Visual C# .NET
For additional information about how to execute XPath queries with the System.Xml classes, click the article number below to view the article in the Microsoft Knowledge Base:
313828 INFO: Roadmap for Executing XPath Queries in .NET Applications
To access data, you typically select or navigate to one or more required nodes and then use the resulting one or more nodes to manipulate the XML content. The following is a list of relevant System.Xml properties and methods:
  • InnerText and InnerXml. Find concatenated values of all child nodes or the markup of all child nodes respectively.
  • Value. Provides access to the value of a node.
  • Attributes. Finds the collection of attributes of the current node.
  • FirstChild, LastChild, and ChildNodes. Provide access to the first node, the last node, or all of the child nodes of the current node.
  • NextSibling and PreviousSibling. Provide access to the nodes immediately follow and immediately precede respectively.
  • DocumentElement. Provides access to the root XmlElement of the document.
  • Item. Finds the specified child element.
  • NodeType (of type XmlNodeType). Indicates the type of the current node; for example Attribute, Element, or Text.
  • XmlNodeType. Enumeration holds all possible node types.
  • GetElementsByTagName and GetElementByID. Provide access to one or more specified XML elements.
You can use several other properties and methods to access XML content, such as Name and NameTable. For the complete list, see the Microsoft .NET Framework SDK documentation.

back to the top

Access Data from XML Documents

The following example shows how to use some of these methods to access XML data. Read the inline comments to understand how the code works.
  1. Use Notepad or a similar text editor to save the following data as a file named C:\Q317664.xml:
    <?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?><Collection>   <Book Id='1' ISBN='1-100000ABC-200'>      <Title>Principle of Relativity</Title>      <!-- Famous physicist -->            <Author>Albert Einstein</Author>      <Genre>Physics</Genre>   </Book>   <Book Id='2' ISBN='1-100000ABC-300'>      <!-- Also came as a TV serial -->      <Title>Cosmos</Title>      <Author>Carl Sagan</Author>      <Genre>Cosmology</Genre>   </Book>   <!-- Add additional books here --></Collection>					
  2. Create a new Visual C# .NET Console Application project.
  3. Replace the code in Class1.cs with the following code. This example loads the XML document from a file representing a collection of Books and then accesses the content using some of the methods mentioned earlier.
    using System;using System.Xml;using System.Text;namespace ConsoleApplication1{   class Class1   {      public static int GetNodeTypeCount(XmlNode node, XmlNodeType nodeType)      {         // Recursively loop through the given node and return          // the number of occurences of a specific nodeType.         int i = 0;         if (node.NodeType == nodeType)            i = i + 1;         if (node.HasChildNodes)            foreach (XmlNode cNode in node.ChildNodes)               i = i + GetNodeTypeCount(cNode, nodeType);         return i;      }      [STAThread]      static void Main(string[] args)      {         try         {            // Create an Xml document instance and load XML data.            XmlDocument doc  = new XmlDocument();            doc.Load("C:\\Q317664.xml");                                // 1. Select all the Book titles by using an XPath query.            XmlNodeList nodeList = doc.SelectNodes("//Book/Title");            XmlNode node;            Console.WriteLine("{0}", "TITLES LIST: ");            foreach (XmlNode nd in nodeList)               Console.WriteLine("{0}", nd.InnerText);            // 2. Read the XmlDeclartion values.            XmlDeclaration decl = (XmlDeclaration) doc.FirstChild;            Console.WriteLine("\n{0}", "XML DECLARTION:");            Console.WriteLine("{0}", "Version    " + "= " + decl.Version);            Console.WriteLine("{0}", "Encoding   " + "= " + decl.Encoding);            Console.WriteLine("{0}", "Standalone " + "= " + decl.Standalone);            // 3. Move to the first node of DOM and get all of its attributes.            XmlElement root = doc.DocumentElement;            node = root.FirstChild;            Console.WriteLine("\n{0}", "ATTRIBUTES OF THE FIRST CHILD:");            foreach  (XmlAttribute attr in node.Attributes)               Console.WriteLine("{0}", attr.Name + " = " + attr.InnerText);            // 4. Navigate to the child nodes of the first Book node.            Console.WriteLine("\n{0}", "FIRST NODE'S CHILDREN:");            if (node.HasChildNodes)               foreach (XmlNode cNode in node.ChildNodes)                  Console.WriteLine("{0}", cNode.OuterXml);            // 5. Navigate to the next sibling of the first Book node.            node = node.NextSibling;            Console.WriteLine("\n{0}", "NEXT SIBLING:");            if (node != null)               Console.WriteLine("{0}", node.OuterXml);                        // 6. Get the parent node details of the current node.            Console.WriteLine("\n{0}", "PARENT NODE NAME = " + node.ParentNode.Name);            Console.WriteLine("{0}", "PARENT NODE HAS " + node.ParentNode.ChildNodes.Count + " CHILD NODES");            Console.WriteLine("{0}", "PARENT NODE'S NAMESPACE URI = " + node.ParentNode.NamespaceURI);            // 7. Count the number of Comment nodes in the document.            // You could search for other types in the same way.            int commentNodes = Class1.GetNodeTypeCount(doc.DocumentElement, XmlNodeType.Comment);            Console.WriteLine("\n{0}\n", "NUMBER OF COMMENT NODES IN THE DOC = " + commentNodes);            Console.ReadLine();         }         catch(XmlException xmlEx)        // Handle the XML Exceptions here.                  {            Console.WriteLine("{0}", xmlEx.Message);         }         catch(Exception ex)              // Handle the Generic Exceptions here.         {            Console.WriteLine("{0}", ex.Message);         }      }   }}					
  4. Compile and then run the application.

    NOTE: The file Q317664.xml should be in the same folder as the executable file (or you can modify the file path in the code).

    The output should resemble the following:
    TITLES LIST:Principle of RelativityCosmosXML DECLARTION:Version    = 1.0Encoding   = ISO-8859-1Standalone = yesATTRIBUTES OF THE FIRST CHILD:Id = 1ISBN = 1-100000ABC-200FIRST NODE'S CHILDREN:<Title>Principle of Relativity</Title><!-- Famous physicist --><Author>Albert Einstein</Author><Genre>Physics</Genre>NEXT SIBLING:<Book Id="2" ISBN="1-100000ABC-300"><!-- Also came as a TV serial --><Title>Cosmos</Title><Author>Carl Sagan</Author><Genre>Cosmology</Genre></Book>PARENT NODE NAME = CollectionPARENT NODE HAS 3 CHILD NODESPARENT NODE'S NAMESPACE URI =NUMBER OF COMMENT NODES IN THE DOC = 3					
back to the top
For additional information, visit the following Microsoft Web sites:For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
313824 INFO: Roadmap for Programming XML with the DOM-Model Parser in the .NET Framework
313651 INFO: Roadmap for XML in the .NET Framework
back to the top

Article ID: 317664 - Last Review: 12/07/2015 08:55:45 - Revision: 4.3

Microsoft .NET Framework 1.1, Microsoft .NET Framework 1.0, Microsoft Visual C# .NET 2002 Standard Edition, Microsoft Visual C# .NET 2003 Standard Edition

  • kbnosurvey kbarchive kbbcl kbhowtomaster KB317664