1 //------------------------------------------------------------------------------
2 // <copyright file="XmlText.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
12 using System.Diagnostics;
13 using System.Xml.XPath;
15 // Represents the text content of an element or attribute.
16 public class XmlText : XmlCharacterData {
17 internal XmlText( string strData ): this( strData, null ) {
20 protected internal XmlText( string strData, XmlDocument doc ): base( strData, doc ) {
23 // Gets the name of the node.
24 public override String Name {
26 return OwnerDocument.strTextName;
30 // Gets the name of the current node without the namespace prefix.
31 public override String LocalName {
33 return OwnerDocument.strTextName;
37 // Gets the type of the current node.
38 public override XmlNodeType NodeType {
40 return XmlNodeType.Text;
44 public override XmlNode ParentNode {
46 switch (parentNode.NodeType) {
47 case XmlNodeType.Document:
49 case XmlNodeType.Text:
50 case XmlNodeType.CDATA:
51 case XmlNodeType.Whitespace:
52 case XmlNodeType.SignificantWhitespace:
53 XmlNode parent = parentNode.parentNode;
54 while (parent.IsText) {
55 parent = parent.parentNode;
64 // Creates a duplicate of this node.
65 public override XmlNode CloneNode(bool deep) {
66 Debug.Assert( OwnerDocument != null );
67 return OwnerDocument.CreateTextNode( Data );
70 public override String Value {
77 XmlNode parent = parentNode;
78 if ( parent != null && parent.NodeType == XmlNodeType.Attribute ) {
79 XmlUnspecifiedAttribute attr = parent as XmlUnspecifiedAttribute;
80 if ( attr != null && !attr.Specified ) {
81 attr.SetSpecified( true );
87 // Splits the node into two nodes at the specified offset, keeping
88 // both in the tree as siblings.
89 public virtual XmlText SplitText(int offset) {
90 XmlNode parentNode = this.ParentNode;
91 int length = this.Length;
93 throw new ArgumentOutOfRangeException( "offset" );
94 //if the text node is out of the living tree, throw exception.
95 if ( parentNode == null )
96 throw new InvalidOperationException(Res.GetString(Res.Xdom_TextNode_SplitText));
98 int count = length - offset;
99 String splitData = Substring(offset, count);
100 DeleteData(offset, count);
101 XmlText newTextNode = OwnerDocument.CreateTextNode(splitData);
102 parentNode.InsertAfter(newTextNode, this);
106 // Saves the node to the specified XmlWriter.
107 public override void WriteTo(XmlWriter w) {
111 // Saves all the children of the node to the specified XmlWriter.
112 public override void WriteContentTo(XmlWriter w) {
113 // Intentionally do nothing
116 internal override XPathNodeType XPNodeType {
118 return XPathNodeType.Text;
122 internal override bool IsText {
128 public override XmlNode PreviousText {
130 if (parentNode.IsText) {