XmlDocumentFragment.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Xml.XmlDocumentFragment
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C), Ximian, Inc
  9. // (C)2002 Atsushi Enomoto
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Text;
  33. using System.Xml.XPath;
  34. namespace System.Xml
  35. {
  36. public class XmlDocumentFragment : XmlNode
  37. {
  38. #region Constructor
  39. protected internal XmlDocumentFragment (XmlDocument doc)
  40. : base (doc)
  41. {
  42. }
  43. #endregion
  44. #region Properties
  45. public override string InnerXml {
  46. set {
  47. // Copied from XmlElement.InnerXml (in the meantime;-))
  48. for (int i = 0; i < ChildNodes.Count; i++)
  49. this.RemoveChild (ChildNodes [i]);
  50. // I hope there are any well-performance logic...
  51. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  52. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  53. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  54. BaseURI, XmlLang, XmlSpace, null);
  55. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  56. xmlReader.XmlResolver = OwnerDocument.Resolver;
  57. do {
  58. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  59. if(n == null) break;
  60. AppendChild (n);
  61. } while (true);
  62. }
  63. get {
  64. StringBuilder sb = new StringBuilder ();
  65. for (int i = 0; i < ChildNodes.Count; i++)
  66. sb.Append (ChildNodes [i].OuterXml);
  67. return sb.ToString ();
  68. }
  69. }
  70. public override string LocalName {
  71. get { return "#document-fragment"; }
  72. }
  73. public override string Name {
  74. get { return "#document-fragment"; }
  75. }
  76. public override XmlNodeType NodeType {
  77. get { return XmlNodeType.DocumentFragment; }
  78. }
  79. public override XmlDocument OwnerDocument {
  80. get { return base.OwnerDocument; }
  81. }
  82. public override XmlNode ParentNode {
  83. get { return null; } // it's always null here.
  84. }
  85. internal override XPathNodeType XPathNodeType
  86. {
  87. get { return XPathNodeType.Root; }
  88. }
  89. #endregion
  90. #region Methods
  91. public override XmlNode CloneNode (bool deep)
  92. {
  93. if (deep) { // clone document + child nodes
  94. XmlNode node = FirstChild;
  95. while ((node != null) && (node.HasChildNodes)) {
  96. AppendChild (node.NextSibling.CloneNode (false));
  97. node = node.NextSibling;
  98. }
  99. return node;
  100. } else
  101. return new XmlDocumentFragment (OwnerDocument);
  102. }
  103. public override void WriteContentTo (XmlWriter w)
  104. {
  105. for (int i = 0; i < ChildNodes.Count; i++)
  106. ChildNodes [i].WriteContentTo (w);
  107. }
  108. public override void WriteTo (XmlWriter w)
  109. {
  110. for (int i = 0; i < ChildNodes.Count; i++)
  111. ChildNodes [i].WriteTo (w);
  112. }
  113. #endregion
  114. }
  115. }