On this page
System.Xml.XmlNode Class
The XmlNode class is the abstract base class for all nodes in an XML Document Object Model (DOM). It provides core properties and methods for navigating, creating, and manipulating XML document structures.
Inheritance
Object ↳ XmlNode
Properties
| Name | Type | Description |
|---|---|---|
| Name | String | Gets the name of the current node. |
| Value | String | Gets or sets the value of the node. |
| ParentNode | XmlNode | The parent of this node (null for the document node). |
| ChildNodes | XmlNodeList | A collection of the child nodes of this node. |
| Attributes | XmlAttributeCollection | Gets the attribute collection if this node is an element. |
| InnerXml | String | Gets or sets the markup representing the child nodes of this node. |
| OuterXml | String | Gets the markup representing this node and its children. |
Methods
| Name | Signature | Description |
|---|---|---|
| AppendChild | XmlNode AppendChild(XmlNode newChild) | Adds the specified node to the end of the child node list. |
| RemoveChild | XmlNode RemoveChild(XmlNode oldChild) | Removes the specified child node from the child node list. |
| SelectNodes | XmlNodeList SelectNodes(string xpath) | Returns a list of nodes matching the XPath expression. |
| SelectSingleNode | XmlNode SelectSingleNode(string xpath) | Returns the first node that matches the XPath expression. |
| CloneNode | XmlNode CloneNode(bool deep) | Creates a duplicate of the node. If deep is true, also clones descendants. |
| WriteContentTo | void WriteContentTo(XmlWriter w) | Writes the child nodes of this node to the specified XmlWriter. |
Examples
The following example creates an XML document, adds elements, and retrieves values using XmlNode methods.
using System;
using System.Xml;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("books");
doc.AppendChild(root);
XmlElement book = doc.CreateElement("book");
book.SetAttribute("id", "b1");
root.AppendChild(book);
XmlElement title = doc.CreateElement("title");
title.InnerText = "Programming C#";
book.AppendChild(title);
Console.WriteLine(doc.OuterXml);
// Select the title node using XPath
XmlNode titleNode = doc.SelectSingleNode("//book/title");
Console.WriteLine($\"Title: {titleNode.InnerText}\");
}
}