XmlCharacterData.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // System.Xml.XmlCharacterData.cs
  3. //
  4. // Authors:
  5. // Jason Diamond <[email protected]>
  6. // Kral Ferch <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond, Kral Ferch
  9. //
  10. using System;
  11. using System.Xml.XPath;
  12. namespace System.Xml
  13. {
  14. public abstract class XmlCharacterData : XmlLinkedNode
  15. {
  16. private string data;
  17. #region Constructor
  18. protected internal XmlCharacterData (string data, XmlDocument doc)
  19. : base (doc)
  20. {
  21. if (data == null)
  22. data = String.Empty;
  23. this.data = data;
  24. }
  25. #endregion
  26. #region Properties
  27. public virtual string Data {
  28. get { return data; }
  29. set {
  30. OwnerDocument.onNodeChanging (this, this.ParentNode);
  31. if (IsReadOnly)
  32. throw new ArgumentException ("Node is read-only.");
  33. data = value;
  34. OwnerDocument.onNodeChanged (this, this.ParentNode);
  35. }
  36. }
  37. public override string InnerText {
  38. get { return data; }
  39. set { Data = value; } // invokes events
  40. }
  41. public virtual int Length {
  42. get { return data != null ? data.Length : 0; }
  43. }
  44. public override string Value {
  45. get { return data; }
  46. set {
  47. Data = value;
  48. }
  49. }
  50. internal override XPathNodeType XPathNodeType {
  51. get { return XPathNodeType.Text; }
  52. }
  53. #endregion
  54. #region Methods
  55. public virtual void AppendData (string strData)
  56. {
  57. OwnerDocument.onNodeChanging (this, this.ParentNode);
  58. data += strData;
  59. OwnerDocument.onNodeChanged (this, this.ParentNode);
  60. }
  61. public virtual void DeleteData (int offset, int count)
  62. {
  63. OwnerDocument.onNodeChanging (this, this.ParentNode);
  64. if (offset < 0)
  65. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  66. int newCount = data.Length - offset;
  67. if ((offset + count) < data.Length)
  68. newCount = count;
  69. data = data.Remove (offset, newCount);
  70. OwnerDocument.onNodeChanged (this, this.ParentNode);
  71. }
  72. public virtual void InsertData (int offset, string strData)
  73. {
  74. OwnerDocument.onNodeChanging (this, this.ParentNode);
  75. if ((offset < 0) || (offset > data.Length))
  76. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  77. data = data.Insert(offset, strData);
  78. OwnerDocument.onNodeChanged (this, this.ParentNode);
  79. }
  80. public virtual void ReplaceData (int offset, int count, string strData)
  81. {
  82. OwnerDocument.onNodeChanging (this, this.ParentNode);
  83. if ((offset < 0) || (offset > data.Length))
  84. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  85. if (count < 0)
  86. throw new ArgumentOutOfRangeException ("count", "Must be non-negative.");
  87. if (strData == null)
  88. throw new ArgumentNullException ("strData", "Must be non-null.");
  89. string newData = data.Substring (0, offset) + strData;
  90. if ((offset + count) < data.Length)
  91. newData += data.Substring (offset + count);
  92. data = newData;
  93. OwnerDocument.onNodeChanged (this, this.ParentNode);
  94. }
  95. public virtual string Substring (int offset, int count)
  96. {
  97. if (data.Length < offset + count)
  98. return data.Substring (offset);
  99. else
  100. return data.Substring (offset, count);
  101. }
  102. #endregion
  103. }
  104. }