XmlDocumentFragment.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, IHasXmlChildNode
  37. {
  38. XmlLinkedNode lastLinkedChild;
  39. #region Constructor
  40. protected internal XmlDocumentFragment (XmlDocument doc)
  41. : base (doc)
  42. {
  43. }
  44. #endregion
  45. #region Properties
  46. XmlLinkedNode IHasXmlChildNode.LastLinkedChild {
  47. get { return lastLinkedChild; }
  48. set { lastLinkedChild = value; }
  49. }
  50. public override string InnerXml {
  51. set {
  52. // Copied from XmlElement.InnerXml (in the meantime;-))
  53. for (int i = 0; i < ChildNodes.Count; i++)
  54. this.RemoveChild (ChildNodes [i]);
  55. // I hope there are any well-performance logic...
  56. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  57. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  58. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  59. BaseURI, XmlLang, XmlSpace, null);
  60. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  61. xmlReader.XmlResolver = OwnerDocument.Resolver;
  62. do {
  63. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  64. if(n == null) break;
  65. AppendChild (n);
  66. } while (true);
  67. }
  68. get {
  69. StringBuilder sb = new StringBuilder ();
  70. for (int i = 0; i < ChildNodes.Count; i++)
  71. sb.Append (ChildNodes [i].OuterXml);
  72. return sb.ToString ();
  73. }
  74. }
  75. public override string LocalName {
  76. get { return "#document-fragment"; }
  77. }
  78. public override string Name {
  79. get { return "#document-fragment"; }
  80. }
  81. public override XmlNodeType NodeType {
  82. get { return XmlNodeType.DocumentFragment; }
  83. }
  84. public override XmlDocument OwnerDocument {
  85. get { return base.OwnerDocument; }
  86. }
  87. public override XmlNode ParentNode {
  88. get { return null; } // it's always null here.
  89. }
  90. internal override XPathNodeType XPathNodeType
  91. {
  92. get { return XPathNodeType.Root; }
  93. }
  94. #endregion
  95. #region Methods
  96. public override XmlNode CloneNode (bool deep)
  97. {
  98. if (deep) { // clone document + child nodes
  99. XmlNode node = FirstChild;
  100. while ((node != null) && (node.HasChildNodes)) {
  101. AppendChild (node.NextSibling.CloneNode (false));
  102. node = node.NextSibling;
  103. }
  104. return node;
  105. } else
  106. return new XmlDocumentFragment (OwnerDocument);
  107. }
  108. public override void WriteContentTo (XmlWriter w)
  109. {
  110. for (int i = 0; i < ChildNodes.Count; i++)
  111. ChildNodes [i].WriteContentTo (w);
  112. }
  113. public override void WriteTo (XmlWriter w)
  114. {
  115. for (int i = 0; i < ChildNodes.Count; i++)
  116. ChildNodes [i].WriteTo (w);
  117. }
  118. #endregion
  119. }
  120. }