XmlCharacterData.cs 2.9 KB

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