XmlNode Value vs InnerText
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
Expected behavior:
- '
book.Valueisnull' - '
book.InnerTextisXML GuideLee' - '
id.Valueis42' - '
titleText.ValueisXML 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:
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
Valueto return element text and being surprised bynull. - Using
InnerTextwhen you actually need XML structure preserved. - Setting
InnerTexton an element and accidentally deleting nested markup. - Assuming whitespace handling will always match visual formatting expectations.
- Reading
InnerTextfrom a large subtree repeatedly without considering traversal cost.
Summary
- '
Valueis for node types that directly store a value.' - Element nodes usually return
nullforValue. - '
InnerTextreturns the combined text content of a node's descendants.' - Setting
InnerTexton an element replaces its child markup with plain text. - Choose
Valuefor attributes and text nodes, andInnerTextfor element text extraction.
Related reading
- XmlSerializer - There was an error reflecting type
- XmlSerializer remove unnecessary xsi and xsd namespaces
- XPath and XSLT 2.0 for .NET?
- xUnit.net Global setup teardown?
- You must add a reference to assembly ''netstandard, Version2.0.0.0
- Your project is not referencing the .NETFramework,Versionv4.5 framework.
- A definitive guide to API-breaking changes in .NET
- A difference in style IDictionary vs Dictionary

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.