XmlCharacterData.cs 3.1 KB

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