XmlText Class
Represents a text node.
Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Inheritance: System.Object > System.Xml.XmlNode > System.Xml.XmlCharacterData > System.Xml.XmlText
Derived classes: XmlCDataSection, XmlWhitespace
Remarks
XmlText
inherits from XmlCharacterData
, which provides methods for accessing and manipulating the value of the text node. When parsing an XML document, text nodes that are not significant whitespace are parsed as XmlText
nodes.
Significant whitespace is parsed as XmlWhitespace
nodes. CData sections are parsed as XmlCDataSection
nodes, which is a derived class of XmlText
.
The Value
property inherited from XmlCharacterData
gets or sets the text content of the node.
Constructors
Initializes a new instance of the XmlText
class.
Initializes a new instance of the XmlText
class with the specified text value and document.
Properties
Gets or sets the text content of this node.
Gets or sets a value indicating whether to load all of the markup contained in the node, and parse it as HTML.
Gets or sets a value indicating whether to load all of the markup contained in the node, and parse it as XML.
Gets the local name of the current node.
Gets the qualified name of the node.
Gets the node type of the current node.
Gets the Uniform Resource Identifier (URI) of the namespace that this node belongs to, or an empty string if it does not have one.
Gets the namespace prefix of the current node.
Gets or sets the text content of this node.
Methods
Appends the specified string to the end of the value of this node.
Creates a copy of this node.
deep
- Determines whether to clone all the children of the node.
true
to clone everything, andfalse
to clone only the node itself.
Inserts the specified string at the specified offset within this node.
offset
- The offset at which to insert the data.
data
- The string to insert.
Removes the specified number of characters from this node, starting at the specified offset.
offset
- The offset at which to start removing characters.
count
- The number of characters to remove.
Replaces the text node, starting at the specified offset, with the specified string.
offset
- The offset at which to start replacing characters.
count
- The number of characters to replace.
data
- The string to replace the characters with.
Returns a portion of the text of this node.
offset
- The starting offset of the substring to return.
count
- The number of characters to return.
Example
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>This is some <b>text</b>.</root>");
// Get the XmlText node representing "This is some "
XmlNode firstTextNode = doc.DocumentElement.FirstChild;
if (firstTextNode is XmlText xmlTextNode)
{
Console.WriteLine($"Value: {xmlTextNode.Value}");
Console.WriteLine($"Node Type: {xmlTextNode.NodeType}");
}
// Get the XmlText node representing "."
XmlNode lastTextNode = doc.DocumentElement.LastChild;
if (lastTextNode is XmlText lastXmlTextNode)
{
Console.WriteLine($"Value: {lastXmlTextNode.Value}");
}
}
}