XPathEditableDocument.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. //
  2. // Mono.Xml.XPath.XPathEditableDocument
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2004 Novell Inc.
  8. //
  9. // Yet another implementation of editable XPathNavigator.
  10. // (Even runnable under MS.NET 2.0)
  11. //
  12. // By rewriting XPathEditableDocument.CreateNavigator() as just to
  13. // create XmlDocumentNavigator, XmlDocumentEditableNavigator could be used
  14. // as to implement XmlDocument.CreateNavigator().
  15. //
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. #if NET_2_0
  37. using System;
  38. using System.Collections;
  39. using System.ComponentModel;
  40. using System.IO;
  41. using System.Xml;
  42. using System.Xml.Schema;
  43. using System.Xml.XPath;
  44. using System.Xml.Serialization;
  45. namespace Mono.Xml.XPath
  46. {
  47. internal class XPathEditableDocument : IXPathNavigable
  48. {
  49. /*
  50. public static void Main ()
  51. {
  52. try {
  53. #if true
  54. XmlDocument doc = new XmlDocument ();
  55. XPathEditableDocument pd = new XPathEditableDocument (doc);
  56. XPathNavigator nav = pd.CreateNavigator ();
  57. IChangeTracking xp = pd;
  58. #else
  59. XPathDocument doc = new XPathDocument ();
  60. XPathNavigator nav = doc.CreateNavigator ();
  61. IChangeTracking xp = doc;
  62. #endif
  63. doc.LoadXml ("<root/>");
  64. nav.MoveToFirstChild (); // root
  65. XmlWriter w = nav.AppendChild ();
  66. Console.WriteLine (((IChangeTracking) xp).IsChanged);
  67. w.WriteElementString ("foo", "foo_text");
  68. w.WriteElementString ("bar", "bar_text");
  69. w.WriteStartElement ("hoge");
  70. w.WriteAttributeString ("fuga", "fugafuga");
  71. w.WriteAttributeString ("unya", "unyaunya");
  72. w.WriteFullEndElement ();
  73. w.Close ();
  74. w = nav.CreateAttributes ();
  75. w.WriteStartAttribute ("namara");
  76. w.WriteString ("mokera");
  77. w.WriteEndAttribute ();
  78. w.WriteAttributeString ("beccho", "giccho");
  79. w.Close ();
  80. nav.MoveToRoot ();
  81. nav.MoveToFirstChild ();
  82. nav.MoveToFirstChild ();
  83. nav.DeleteSelf (); // delete foo
  84. Console.WriteLine (nav.Name);
  85. nav.MoveToNext ();
  86. Console.WriteLine (nav.Name);
  87. Console.WriteLine (nav.MoveToFirstAttribute ());
  88. nav.DeleteSelf (); // delete fuga
  89. doc.Save (Console.Out);
  90. } catch (Exception ex) {
  91. Console.WriteLine (ex);
  92. }
  93. }
  94. */
  95. XmlNode node;
  96. ArrayList changes = new ArrayList ();
  97. public XPathEditableDocument (XmlNode node)
  98. {
  99. this.node = node;
  100. }
  101. public virtual bool CanEdit {
  102. get { return true; }
  103. }
  104. public XmlNode Node {
  105. get { return node; }
  106. }
  107. public XPathNavigator CreateNavigator ()
  108. {
  109. return new XmlDocumentEditableNavigator (this);
  110. }
  111. public XmlWriter CreateWriter ()
  112. {
  113. return CreateNavigator ().AppendChild ();
  114. }
  115. public bool HasChanges ()
  116. {
  117. return IsChanged;
  118. }
  119. #region IRevertibleChangeTracking/IChangeTracking
  120. public bool IsChanged {
  121. get { return changes.Count != 0; }
  122. }
  123. public void AcceptChanges ()
  124. {
  125. changes.Clear ();
  126. }
  127. public void RejectChanges ()
  128. {
  129. for (int i = changes.Count - 1; i >= 0; i--) {
  130. Insertion ins = changes [i] as Insertion;
  131. if (ins != null) {
  132. ins.ParentNode.RemoveChild (ins.InsertedNode);
  133. continue;
  134. }
  135. Removal rem = changes [i] as Removal;
  136. if (rem != null) {
  137. if (rem.RemovedNode.NodeType == XmlNodeType.Attribute) {
  138. XmlElement el = (XmlElement) rem.OwnerNode;
  139. el.SetAttributeNode ((XmlAttribute) rem.RemovedNode);
  140. }
  141. else
  142. rem.OwnerNode.InsertBefore (rem.RemovedNode, rem.NextSibling);
  143. continue;
  144. }
  145. AttributeUpdate au = changes [i] as AttributeUpdate;
  146. if (au != null) {
  147. if (au.OldAttribute != null)
  148. au.Element.SetAttributeNode (au.OldAttribute);
  149. else
  150. au.Element.RemoveAttributeNode (au.NewAttribute);
  151. continue;
  152. }
  153. }
  154. changes.Clear ();
  155. }
  156. #endregion
  157. #region IXmlSerializable
  158. public void WriteXml (XmlWriter writer)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. public void ReadXml (XmlReader reader)
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. public XmlSchema GetSchema ()
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. #endregion
  171. internal bool DeleteNode (XmlNode node)
  172. {
  173. Removal rem = new Removal ();
  174. if (node.NodeType == XmlNodeType.Attribute) {
  175. XmlAttribute attr = node as XmlAttribute;
  176. rem.OwnerNode = attr.OwnerElement;
  177. rem.RemovedNode = node;
  178. attr.OwnerElement.RemoveAttributeNode (attr);
  179. return false;
  180. } else {
  181. rem.OwnerNode = node.ParentNode;
  182. rem.NextSibling = node.NextSibling;
  183. rem.RemovedNode = node;
  184. node.ParentNode.RemoveChild (node);
  185. return rem.NextSibling != null;
  186. }
  187. }
  188. internal XmlWriter CreateInsertionWriter (XmlNode owner, XmlNode previousSibling)
  189. {
  190. return new XmlDocumentInsertionWriter (owner, previousSibling, this);
  191. }
  192. internal XmlWriter CreateAttributesWriter (XmlNode owner)
  193. {
  194. return new XmlDocumentAttributeWriter (owner, this);
  195. }
  196. internal void AttributeUpdate (XmlElement element, XmlAttribute oldAttr, XmlAttribute newAttr)
  197. {
  198. AttributeUpdate au = new AttributeUpdate ();
  199. au.Element = element;
  200. au.OldAttribute = oldAttr;
  201. au.NewAttribute = newAttr;
  202. changes.Add (au);
  203. }
  204. internal void AppendChild (XmlNode parent, XmlNode child)
  205. {
  206. Insertion ins = new Insertion ();
  207. ins.ParentNode = parent;
  208. ins.InsertedNode = child;
  209. changes.Add (ins);
  210. }
  211. }
  212. internal class XmlDocumentInsertionWriter : XmlWriter
  213. {
  214. XmlNode current;
  215. XmlNode previousSibling;
  216. XPathEditableDocument document;
  217. Stack nodeStack = new Stack ();
  218. public XmlDocumentInsertionWriter (XmlNode owner, XmlNode previousSibling, XPathEditableDocument doc)
  219. {
  220. this.current = (XmlNode) owner;
  221. if (current == null)
  222. throw new InvalidOperationException ();
  223. this.previousSibling = previousSibling;
  224. this.document = doc;
  225. state = WriteState.Content;
  226. }
  227. WriteState state;
  228. XmlAttribute attribute;
  229. public override WriteState WriteState {
  230. get { return state; }
  231. }
  232. public override void Close ()
  233. {
  234. }
  235. public override void Flush ()
  236. {
  237. }
  238. public override string LookupPrefix (string ns)
  239. {
  240. return current.GetPrefixOfNamespace (ns);
  241. }
  242. public override void WriteStartAttribute (string prefix, string name, string ns)
  243. {
  244. if (state != WriteState.Content)
  245. throw new InvalidOperationException ("Current state is not inside element. Cannot start attribute.");
  246. attribute = current.OwnerDocument.CreateAttribute (prefix, name, ns);
  247. state = WriteState.Attribute;
  248. }
  249. public override void WriteProcessingInstruction (string name, string value)
  250. {
  251. XmlProcessingInstruction pi = current.OwnerDocument.CreateProcessingInstruction (name, value);
  252. current.AppendChild (pi);
  253. document.AppendChild (current, pi);
  254. }
  255. public override void WriteComment (string text)
  256. {
  257. XmlComment comment = current.OwnerDocument.CreateComment (text);
  258. current.AppendChild (comment);
  259. document.AppendChild (current, comment);
  260. }
  261. public override void WriteCData (string text)
  262. {
  263. XmlCDataSection cdata = current.OwnerDocument.CreateCDataSection (text);
  264. current.AppendChild (cdata);
  265. document.AppendChild (current, cdata);
  266. }
  267. public override void WriteStartElement (string prefix, string name, string ns)
  268. {
  269. XmlDocument doc = current.OwnerDocument;
  270. if (doc == null)
  271. doc = current as XmlDocument;
  272. if (doc == null)
  273. throw new SystemException ("Should not happen.");
  274. XmlElement el = doc.CreateElement (prefix, name, ns);
  275. current.AppendChild (el);
  276. document.AppendChild (current, el);
  277. nodeStack.Push (current);
  278. current = el;
  279. }
  280. public override void WriteEndElement ()
  281. {
  282. if (nodeStack.Count == 0)
  283. throw new InvalidOperationException ("No element is opened.");
  284. current = nodeStack.Pop () as XmlNode;
  285. }
  286. public override void WriteFullEndElement ()
  287. {
  288. WriteEndElement ();
  289. XmlElement el = current as XmlElement;
  290. if (el != null)
  291. el.IsEmpty = false;
  292. }
  293. public override void WriteDocType (string name, string pubid, string systemId, string intsubset)
  294. {
  295. throw new NotSupportedException ();
  296. }
  297. public override void WriteStartDocument ()
  298. {
  299. throw new NotSupportedException ();
  300. }
  301. public override void WriteStartDocument (bool standalone)
  302. {
  303. throw new NotSupportedException ();
  304. }
  305. public override void WriteEndDocument ()
  306. {
  307. throw new NotSupportedException ();
  308. }
  309. public override void WriteBase64 (byte [] data, int start, int length)
  310. {
  311. WriteString (Convert.ToBase64String (data, start, length));
  312. }
  313. public override void WriteRaw (char [] raw, int start, int length)
  314. {
  315. throw new NotSupportedException ();
  316. }
  317. public override void WriteRaw (string raw)
  318. {
  319. throw new NotSupportedException ();
  320. }
  321. public override void WriteSurrogateCharEntity (char msb, char lsb)
  322. {
  323. throw new NotSupportedException ();
  324. }
  325. public override void WriteCharEntity (char c)
  326. {
  327. throw new NotSupportedException ();
  328. }
  329. public override void WriteEntityRef (string entname)
  330. {
  331. if (state != WriteState.Attribute)
  332. throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
  333. attribute.AppendChild (attribute.OwnerDocument.CreateEntityReference (entname));
  334. }
  335. public override void WriteChars (char [] data, int start, int length)
  336. {
  337. WriteString (new string (data, start, length));
  338. }
  339. public override void WriteString (string text)
  340. {
  341. if (attribute != null)
  342. attribute.Value += text;
  343. else {
  344. XmlText t = current.OwnerDocument.CreateTextNode (text);
  345. current.AppendChild (t);
  346. document.AppendChild (current, t);
  347. }
  348. }
  349. public override void WriteWhitespace (string text)
  350. {
  351. if (state != WriteState.Attribute)
  352. current.AppendChild (current.OwnerDocument.CreateTextNode (text));
  353. else if (attribute.ChildNodes.Count == 0)
  354. attribute.AppendChild (attribute.OwnerDocument.CreateWhitespace (text));
  355. else
  356. attribute.Value += text;
  357. }
  358. public override void WriteEndAttribute ()
  359. {
  360. XmlElement element = current as XmlElement;
  361. if (state != WriteState.Attribute || element == null)
  362. throw new InvalidOperationException ("Current state is not inside attribute. Cannot close attribute.");
  363. document.AttributeUpdate (element, element.SetAttributeNode (attribute), attribute);
  364. attribute = null;
  365. state = WriteState.Content;
  366. }
  367. }
  368. internal class XmlDocumentAttributeWriter : XmlWriter
  369. {
  370. XmlElement element;
  371. XPathEditableDocument document;
  372. public XmlDocumentAttributeWriter (XmlNode owner, XPathEditableDocument doc)
  373. {
  374. element = owner as XmlElement;
  375. if (element == null)
  376. throw new ArgumentException ("To write attributes, current node must be an element.");
  377. state = WriteState.Content;
  378. document = doc;
  379. }
  380. WriteState state;
  381. XmlAttribute attribute;
  382. public override WriteState WriteState {
  383. get { return state; }
  384. }
  385. public override void Close ()
  386. {
  387. }
  388. public override void Flush ()
  389. {
  390. }
  391. public override string LookupPrefix (string ns)
  392. {
  393. return element.GetPrefixOfNamespace (ns);
  394. }
  395. public override void WriteStartAttribute (string prefix, string name, string ns)
  396. {
  397. if (state != WriteState.Content)
  398. throw new InvalidOperationException ("Current state is not inside element. Cannot start attribute.");
  399. attribute = element.OwnerDocument.CreateAttribute (prefix, name, ns);
  400. state = WriteState.Attribute;
  401. }
  402. public override void WriteProcessingInstruction (string name, string value)
  403. {
  404. throw new NotSupportedException ();
  405. }
  406. public override void WriteComment (string text)
  407. {
  408. throw new NotSupportedException ();
  409. }
  410. public override void WriteCData (string text)
  411. {
  412. throw new NotSupportedException ();
  413. }
  414. public override void WriteStartElement (string prefix, string name, string ns)
  415. {
  416. throw new NotSupportedException ();
  417. }
  418. public override void WriteEndElement ()
  419. {
  420. throw new NotSupportedException ();
  421. }
  422. public override void WriteFullEndElement ()
  423. {
  424. throw new NotSupportedException ();
  425. }
  426. public override void WriteDocType (string name, string pubid, string systemId, string intsubset)
  427. {
  428. throw new NotSupportedException ();
  429. }
  430. public override void WriteStartDocument ()
  431. {
  432. throw new NotSupportedException ();
  433. }
  434. public override void WriteStartDocument (bool standalone)
  435. {
  436. throw new NotSupportedException ();
  437. }
  438. public override void WriteEndDocument ()
  439. {
  440. throw new NotSupportedException ();
  441. }
  442. public override void WriteBase64 (byte [] data, int start, int length)
  443. {
  444. throw new NotSupportedException ();
  445. }
  446. public override void WriteRaw (char [] raw, int start, int length)
  447. {
  448. throw new NotSupportedException ();
  449. }
  450. public override void WriteRaw (string raw)
  451. {
  452. throw new NotSupportedException ();
  453. }
  454. public override void WriteSurrogateCharEntity (char msb, char lsb)
  455. {
  456. throw new NotSupportedException ();
  457. }
  458. public override void WriteCharEntity (char c)
  459. {
  460. throw new NotSupportedException ();
  461. }
  462. public override void WriteEntityRef (string entname)
  463. {
  464. if (state != WriteState.Attribute)
  465. throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
  466. attribute.AppendChild (attribute.OwnerDocument.CreateEntityReference (entname));
  467. }
  468. public override void WriteChars (char [] data, int start, int length)
  469. {
  470. WriteString (new string (data, start, length));
  471. }
  472. public override void WriteString (string text)
  473. {
  474. if (state != WriteState.Attribute)
  475. throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
  476. attribute.Value += text;
  477. }
  478. public override void WriteWhitespace (string text)
  479. {
  480. if (state != WriteState.Attribute)
  481. throw new InvalidOperationException ("Current state is not inside attribute. Cannot write attribute value.");
  482. if (attribute.ChildNodes.Count == 0)
  483. attribute.AppendChild (attribute.OwnerDocument.CreateWhitespace (text));
  484. else
  485. attribute.Value += text;
  486. }
  487. public override void WriteEndAttribute ()
  488. {
  489. if (state != WriteState.Attribute)
  490. throw new InvalidOperationException ("Current state is not inside attribute. Cannot close attribute.");
  491. document.AttributeUpdate (element, element.SetAttributeNode (attribute), attribute);
  492. attribute = null;
  493. state = WriteState.Content;
  494. }
  495. }
  496. internal class Insertion
  497. {
  498. // AppendChild : last child / true
  499. // InsertBefore : current node / false
  500. // InsertAfter : current node / true
  501. // PrependChild : first child / false
  502. public XmlNode ParentNode;
  503. public XmlNode InsertedNode;
  504. public bool Afterward;
  505. }
  506. internal class Removal
  507. {
  508. public XmlNode OwnerNode;
  509. public XmlNode NextSibling;
  510. public XmlNode RemovedNode;
  511. }
  512. internal class AttributeUpdate
  513. {
  514. public XmlElement Element;
  515. public XmlAttribute NewAttribute;
  516. public XmlAttribute OldAttribute;
  517. }
  518. internal class XmlDocumentEditableNavigator : XPathNavigator, IHasXmlNode
  519. {
  520. static readonly bool isXmlDocumentNavigatorImpl;
  521. static XmlDocumentEditableNavigator ()
  522. {
  523. isXmlDocumentNavigatorImpl =
  524. (typeof (XmlDocumentEditableNavigator).Assembly
  525. == typeof (XmlDocument).Assembly);
  526. }
  527. XPathEditableDocument document;
  528. XPathNavigator navigator;
  529. public XmlDocumentEditableNavigator (XPathEditableDocument doc)
  530. {
  531. document = doc;
  532. if (isXmlDocumentNavigatorImpl)
  533. navigator = new XmlDocumentNavigator (doc.Node);
  534. else
  535. navigator = doc.CreateNavigator ();
  536. }
  537. public XmlDocumentEditableNavigator (XmlDocumentEditableNavigator nav)
  538. {
  539. document = nav.document;
  540. navigator = nav.navigator.Clone ();
  541. }
  542. public override string BaseURI {
  543. get { return navigator.BaseURI; }
  544. }
  545. public override bool IsEmptyElement {
  546. get { return navigator.IsEmptyElement; }
  547. }
  548. public override string LocalName {
  549. get { return navigator.LocalName; }
  550. }
  551. public override XmlNameTable NameTable {
  552. get { return navigator.NameTable; }
  553. }
  554. public override string Name {
  555. get { return navigator.Name; }
  556. }
  557. public override string NamespaceURI {
  558. get { return navigator.NamespaceURI; }
  559. }
  560. public override XPathNodeType NodeType {
  561. get { return navigator.NodeType; }
  562. }
  563. public override string Prefix {
  564. get { return navigator.Prefix; }
  565. }
  566. public override string Value {
  567. get { return navigator.Value; }
  568. }
  569. public override XPathNavigator Clone ()
  570. {
  571. return new XmlDocumentEditableNavigator (this);
  572. }
  573. public override XPathNavigator CreateNavigator ()
  574. {
  575. return navigator.Clone ();
  576. }
  577. public XmlNode GetNode ()
  578. {
  579. return ((IHasXmlNode) navigator).GetNode ();
  580. }
  581. public override bool IsSamePosition (XPathNavigator other)
  582. {
  583. XmlDocumentEditableNavigator nav = other as XmlDocumentEditableNavigator;
  584. if (nav != null)
  585. return navigator.IsSamePosition (nav.navigator);
  586. else
  587. return navigator.IsSamePosition (nav);
  588. }
  589. public override bool MoveTo (XPathNavigator other)
  590. {
  591. XmlDocumentEditableNavigator nav = other as XmlDocumentEditableNavigator;
  592. if (nav != null)
  593. return navigator.MoveTo (nav.navigator);
  594. else
  595. return navigator.MoveTo (nav);
  596. }
  597. public override bool MoveToFirstAttribute ()
  598. {
  599. return navigator.MoveToFirstAttribute ();
  600. }
  601. public override bool MoveToFirstChild ()
  602. {
  603. return navigator.MoveToFirstChild ();
  604. }
  605. public override bool MoveToFirstNamespace (XPathNamespaceScope scope)
  606. {
  607. return navigator.MoveToFirstNamespace (scope);
  608. }
  609. public override bool MoveToId (string id)
  610. {
  611. return navigator.MoveToId (id);
  612. }
  613. public override bool MoveToNext ()
  614. {
  615. return navigator.MoveToNext ();
  616. }
  617. public override bool MoveToNextAttribute ()
  618. {
  619. return navigator.MoveToNextAttribute ();
  620. }
  621. public override bool MoveToNextNamespace (XPathNamespaceScope scope)
  622. {
  623. return navigator.MoveToNextNamespace (scope);
  624. }
  625. public override bool MoveToParent ()
  626. {
  627. return navigator.MoveToParent ();
  628. }
  629. public override bool MoveToPrevious ()
  630. {
  631. return navigator.MoveToPrevious ();
  632. }
  633. public override XmlWriter AppendChild ()
  634. {
  635. XmlNode n = ((IHasXmlNode) navigator).GetNode ();
  636. if (n == null)
  637. throw new InvalidOperationException ("Should not happen.");
  638. return document.CreateInsertionWriter (n, null);
  639. }
  640. public override XmlWriter InsertBefore ()
  641. {
  642. XmlNode n = ((IHasXmlNode) navigator).GetNode ();
  643. return document.CreateInsertionWriter (n.ParentNode, n.PreviousSibling);
  644. }
  645. public override XmlWriter CreateAttributes ()
  646. {
  647. XmlNode n = ((IHasXmlNode) navigator).GetNode ();
  648. return document.CreateInsertionWriter (n, null);
  649. }
  650. public override void DeleteSelf ()
  651. {
  652. XmlNode n = ((IHasXmlNode) navigator).GetNode ();
  653. if (!navigator.MoveToNext ())
  654. navigator.MoveToParent ();
  655. document.DeleteNode (n);
  656. }
  657. public override void SetValue (string value)
  658. {
  659. XmlNode n = ((IHasXmlNode) navigator).GetNode ();
  660. foreach (XmlNode c in n.ChildNodes)
  661. document.DeleteNode (c);
  662. XmlWriter w = document.CreateInsertionWriter (n, null);
  663. w.WriteValue (value);
  664. w.Close ();
  665. }
  666. }
  667. }
  668. #endif