To illustrate traversing an XmlDocument, we'll use the following XML file:
Jack
Smith
The InnerText property represents the concatenation of all child text nodes.
The following two lines both output Jim, since our XML document contains only a single text node:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("customer.xml");
Console.WriteLine(doc.DocumentElement.ChildNodes[0].InnerText);
Console.WriteLine(doc.DocumentElement.ChildNodes[0].FirstChild.Value);
}
}
The output:
Jack
Jack
Setting the InnerText property replaces all child nodes with a single text node.
Be careful when setting InnerText to not accidentally wipe over element nodes.
For example:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("customer.xml");
doc.DocumentElement.ChildNodes[0].FirstChild.InnerText = "NewValue";
}
}