XmlCharacterData.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 { data = value; }
  29. }
  30. public override string InnerText {
  31. get { return data; }
  32. set { data = value; }
  33. }
  34. public virtual int Length {
  35. get { return data != null ? data.Length : 0; }
  36. }
  37. public override string Value {
  38. get { return data; }
  39. set {
  40. OwnerDocument.onNodeChanging (this, this.ParentNode);
  41. if (IsReadOnly)
  42. throw new ArgumentException ("Node is read-only.");
  43. data = value;
  44. OwnerDocument.onNodeChanged (this, this.ParentNode);
  45. }
  46. }
  47. #endregion
  48. #region Methods
  49. public virtual void AppendData (string strData)
  50. {
  51. OwnerDocument.onNodeChanging (this, this.ParentNode);
  52. data += strData;
  53. OwnerDocument.onNodeChanged (this, this.ParentNode);
  54. }
  55. public virtual void DeleteData (int offset, int count)
  56. {
  57. OwnerDocument.onNodeChanging (this, this.ParentNode);
  58. if (offset < 0)
  59. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  60. int newCount = data.Length - offset;
  61. if ((offset + count) < data.Length)
  62. newCount = count;
  63. data = data.Remove (offset, newCount);
  64. OwnerDocument.onNodeChanged (this, this.ParentNode);
  65. }
  66. public virtual void InsertData (int offset, string strData)
  67. {
  68. OwnerDocument.onNodeChanging (this, this.ParentNode);
  69. if ((offset < 0) || (offset > data.Length))
  70. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  71. data = data.Insert(offset, strData);
  72. OwnerDocument.onNodeChanged (this, this.ParentNode);
  73. }
  74. public virtual void ReplaceData (int offset, int count, string strData)
  75. {
  76. OwnerDocument.onNodeChanging (this, this.ParentNode);
  77. if ((offset < 0) || (offset > data.Length))
  78. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  79. if (strData == null)
  80. throw new ArgumentNullException ("strData", "Must be non-null.");
  81. string newData = data.Substring (0, offset) + strData;
  82. if ((offset + count) < data.Length)
  83. newData += data.Substring (offset + count);
  84. data = newData;
  85. OwnerDocument.onNodeChanged (this, this.ParentNode);
  86. }
  87. public virtual string Substring (int offset, int count)
  88. {
  89. return data.Substring (offset, count);
  90. }
  91. #endregion
  92. }
  93. }