XML
XmlNode
programming
InnerText
C#

XmlNode Value vs InnerText

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

XmlNode.Value and XmlNode.InnerText are not interchangeable. Value is meaningful only for node types that directly carry a value, while InnerText returns the combined text content contained inside a node and its descendants.

That distinction is easy to miss because both sound like "give me the text." In practice, one is node-type specific and the other is subtree-oriented.

What Value Means

XmlNode.Value applies to nodes whose content is their value:

  • attributes
  • text nodes
  • comments
  • CDATA sections

For element nodes, Value is typically null because an element's content is represented by child nodes rather than by a direct scalar value.

What InnerText Means

InnerText walks through the node's descendants and concatenates their text content. If you call it on an element, you get the readable text inside that element, regardless of whether the text is split across nested nodes.

That means InnerText is often what you want when extracting human-readable content from an XML element.

A Concrete Example

csharp
1using System;
2using System.Xml;
3
4public static class Program
5{
6    public static void Main()
7    {
8        var xml = "<book id=\"42\"><title>XML Guide</title><author>Lee</author></book>";
9        var doc = new XmlDocument();
10        doc.LoadXml(xml);
11
12        XmlNode book = doc.DocumentElement!;
13        XmlNode title = book.SelectSingleNode("title")!;
14        XmlAttribute id = book.Attributes!["id"]!;
15        XmlNode titleText = title.FirstChild!;
16
17        Console.WriteLine(book.Value == null ? "null" : book.Value);
18        Console.WriteLine(book.InnerText);
19        Console.WriteLine(id.Value);
20        Console.WriteLine(titleText.Value);
21    }
22}

Expected behavior:

  • 'book.Value is null'
  • 'book.InnerText is XML GuideLee'
  • 'id.Value is 42'
  • 'titleText.Value is XML Guide'

That output shows the main rule clearly: Value depends on node type, but InnerText reads textual content from the subtree.

Setting the Properties

The two properties also differ when you assign to them.

Setting InnerText on an element replaces the element's children with a text node. Setting Value only makes sense on nodes that support direct values, such as attributes or text nodes.

Example:

csharp
1using System;
2using System.Xml;
3
4public static class Program
5{
6    public static void Main()
7    {
8        var doc = new XmlDocument();
9        doc.LoadXml("<item code=\"A1\"><name>Old</name></item>");
10
11        XmlNode item = doc.DocumentElement!;
12        item.InnerText = "Replaced";
13        item.Attributes!["code"]!.Value = "B2";
14
15        Console.WriteLine(doc.OuterXml);
16    }
17}

After this runs, the nested name element is gone because InnerText replaced the child content with plain text.

Which One Should You Use

Use Value when you know you are dealing with:

  • an attribute node
  • a text node
  • a comment or CDATA node

Use InnerText when you want the text content of an element and do not care about the internal XML structure.

If structure matters, use child nodes or XPath queries instead of flattening everything into InnerText.

InnerText Versus Structured Access

InnerText is convenient, but it is lossy. If the XML contains nested formatting, mixed content, or repeated child elements, flattening the subtree into one string may discard exactly the structure you needed. In those cases, navigate the child nodes explicitly or use SelectSingleNode and SelectNodes to target the element or attribute you actually want instead of extracting all descendant text at once.

Common Pitfalls

  • Expecting Value to return element text and being surprised by null.
  • Using InnerText when you actually need XML structure preserved.
  • Setting InnerText on an element and accidentally deleting nested markup.
  • Assuming whitespace handling will always match visual formatting expectations.
  • Reading InnerText from a large subtree repeatedly without considering traversal cost.

Summary

  • 'Value is for node types that directly store a value.'
  • Element nodes usually return null for Value.
  • 'InnerText returns the combined text content of a node's descendants.'
  • Setting InnerText on an element replaces its child markup with plain text.
  • Choose Value for attributes and text nodes, and InnerText for element text extraction.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.