1 //------------------------------------------------------------------------------ 2 // <copyright file="XmlText.cs" company="Microsoft"> 3 // Copyright (c) Microsoft Corporation. All rights reserved. 4 // </copyright> 5 // <owner current="true" primary="true">Microsoft</owner> 6 //------------------------------------------------------------------------------ 7 8 namespace System.Xml 9 { 10 using System; 11 using System.Text; 12 using System.Diagnostics; 13 using System.Xml.XPath; 14 15 // Represents the text content of an element or attribute. 16 public class XmlText : XmlCharacterData { XmlText( string strData )17 internal XmlText( string strData ): this( strData, null ) { 18 } 19 XmlText( string strData, XmlDocument doc )20 protected internal XmlText( string strData, XmlDocument doc ): base( strData, doc ) { 21 } 22 23 // Gets the name of the node. 24 public override String Name { 25 get { 26 return OwnerDocument.strTextName; 27 } 28 } 29 30 // Gets the name of the current node without the namespace prefix. 31 public override String LocalName { 32 get { 33 return OwnerDocument.strTextName; 34 } 35 } 36 37 // Gets the type of the current node. 38 public override XmlNodeType NodeType { 39 get { 40 return XmlNodeType.Text; 41 } 42 } 43 44 public override XmlNode ParentNode { 45 get { 46 switch (parentNode.NodeType) { 47 case XmlNodeType.Document: 48 return null; 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; 56 } 57 return parent; 58 default: 59 return parentNode; 60 } 61 } 62 } 63 64 // Creates a duplicate of this node. CloneNode(bool deep)65 public override XmlNode CloneNode(bool deep) { 66 Debug.Assert( OwnerDocument != null ); 67 return OwnerDocument.CreateTextNode( Data ); 68 } 69 70 public override String Value { 71 get { 72 return Data; 73 } 74 75 set { 76 Data = 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 ); 82 } 83 } 84 } 85 } 86 87 // Splits the node into two nodes at the specified offset, keeping 88 // both in the tree as siblings. SplitText(int offset)89 public virtual XmlText SplitText(int offset) { 90 XmlNode parentNode = this.ParentNode; 91 int length = this.Length; 92 if( offset > 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)); 97 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); 103 return newTextNode; 104 } 105 106 // Saves the node to the specified XmlWriter. WriteTo(XmlWriter w)107 public override void WriteTo(XmlWriter w) { 108 w.WriteString(Data); 109 } 110 111 // Saves all the children of the node to the specified XmlWriter. WriteContentTo(XmlWriter w)112 public override void WriteContentTo(XmlWriter w) { 113 // Intentionally do nothing 114 } 115 116 internal override XPathNodeType XPNodeType { 117 get { 118 return XPathNodeType.Text; 119 } 120 } 121 122 internal override bool IsText { 123 get { 124 return true; 125 } 126 } 127 128 public override XmlNode PreviousText { 129 get { 130 if (parentNode.IsText) { 131 return parentNode; 132 } 133 return null; 134 } 135 } 136 } 137 } 138