File: HtmlNodeNavigator.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (768 lines) | stat: -rw-r--r-- 28,405 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace HtmlAgilityPack
{
    /// <summary>
    /// Represents an HTML navigator on an HTML document seen as a data store.
    /// </summary>
    public class HtmlNodeNavigator : XPathNavigator
    {
        #region Fields

        private int _attindex;
        private HtmlNode _currentnode;
        private HtmlDocument _doc = new HtmlDocument();
        private HtmlNameTable _nametable = new HtmlNameTable();

        internal bool Trace;

        #endregion

        #region Constructors

        internal HtmlNodeNavigator()
        {
            Reset();
        }

        internal HtmlNodeNavigator(HtmlDocument doc, HtmlNode currentNode)
        {
            if (currentNode == null)
            {
                throw new ArgumentNullException("currentNode");
            }
            if (currentNode.OwnerDocument != doc)
            {
                throw new ArgumentException(HtmlDocument.HtmlExceptionRefNotChild);
            }
            InternalTrace(null);

            _doc = doc;
            Reset();
            _currentnode = currentNode;
        }

        private HtmlNodeNavigator(HtmlNodeNavigator nav)
        {
            if (nav == null)
            {
                throw new ArgumentNullException("nav");
            }
            InternalTrace(null);

            _doc = nav._doc;
            _currentnode = nav._currentnode;
            _attindex = nav._attindex;
            _nametable = nav._nametable; // REVIEW: should we do this?
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a stream.
        /// </summary>
        /// <param name="stream">The input stream.</param>
        public HtmlNodeNavigator(Stream stream)
        {
            _doc.Load(stream);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a stream.
        /// </summary>
        /// <param name="stream">The input stream.</param>
        /// <param name="detectEncodingFromByteOrderMarks">Indicates whether to look for byte order marks at the beginning of the stream.</param>
        public HtmlNodeNavigator(Stream stream, bool detectEncodingFromByteOrderMarks)
        {
            _doc.Load(stream, detectEncodingFromByteOrderMarks);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a stream.
        /// </summary>
        /// <param name="stream">The input stream.</param>
        /// <param name="encoding">The character encoding to use.</param>
        public HtmlNodeNavigator(Stream stream, Encoding encoding)
        {
            _doc.Load(stream, encoding);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a stream.
        /// </summary>
        /// <param name="stream">The input stream.</param>
        /// <param name="encoding">The character encoding to use.</param>
        /// <param name="detectEncodingFromByteOrderMarks">Indicates whether to look for byte order marks at the beginning of the stream.</param>
        public HtmlNodeNavigator(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)
        {
            _doc.Load(stream, encoding, detectEncodingFromByteOrderMarks);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a stream.
        /// </summary>
        /// <param name="stream">The input stream.</param>
        /// <param name="encoding">The character encoding to use.</param>
        /// <param name="detectEncodingFromByteOrderMarks">Indicates whether to look for byte order marks at the beginning of the stream.</param>
        /// <param name="buffersize">The minimum buffer size.</param>
        public HtmlNodeNavigator(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int buffersize)
        {
            _doc.Load(stream, encoding, detectEncodingFromByteOrderMarks, buffersize);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a TextReader.
        /// </summary>
        /// <param name="reader">The TextReader used to feed the HTML data into the document.</param>
        public HtmlNodeNavigator(TextReader reader)
        {
            _doc.Load(reader);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a file.
        /// </summary>
        /// <param name="path">The complete file path to be read.</param>
        public HtmlNodeNavigator(string path)
        {
            _doc.Load(path);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a file.
        /// </summary>
        /// <param name="path">The complete file path to be read.</param>
        /// <param name="detectEncodingFromByteOrderMarks">Indicates whether to look for byte order marks at the beginning of the file.</param>
        public HtmlNodeNavigator(string path, bool detectEncodingFromByteOrderMarks)
        {
            _doc.Load(path, detectEncodingFromByteOrderMarks);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a file.
        /// </summary>
        /// <param name="path">The complete file path to be read.</param>
        /// <param name="encoding">The character encoding to use.</param>
        public HtmlNodeNavigator(string path, Encoding encoding)
        {
            _doc.Load(path, encoding);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a file.
        /// </summary>
        /// <param name="path">The complete file path to be read.</param>
        /// <param name="encoding">The character encoding to use.</param>
        /// <param name="detectEncodingFromByteOrderMarks">Indicates whether to look for byte order marks at the beginning of the file.</param>
        public HtmlNodeNavigator(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)
        {
            _doc.Load(path, encoding, detectEncodingFromByteOrderMarks);
            Reset();
        }

        /// <summary>
        /// Initializes a new instance of the HtmlNavigator and loads an HTML document from a file.
        /// </summary>
        /// <param name="path">The complete file path to be read.</param>
        /// <param name="encoding">The character encoding to use.</param>
        /// <param name="detectEncodingFromByteOrderMarks">Indicates whether to look for byte order marks at the beginning of the file.</param>
        /// <param name="buffersize">The minimum buffer size.</param>
        public HtmlNodeNavigator(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int buffersize)
        {
            _doc.Load(path, encoding, detectEncodingFromByteOrderMarks, buffersize);
            Reset();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the base URI for the current node.
        /// Always returns string.Empty in the case of HtmlNavigator implementation.
        /// </summary>
        public override string BaseURI
        {
            get
            {
                InternalTrace(">");
                return _nametable.GetOrAdd(string.Empty);
            }
        }

        /// <summary>
        /// Gets the current HTML document.
        /// </summary>
        public HtmlDocument CurrentDocument
        {
            get { return _doc; }
        }

        /// <summary>
        /// Gets the current HTML node.
        /// </summary>
        public HtmlNode CurrentNode
        {
            get { return _currentnode; }
        }

        /// <summary>
        /// Gets a value indicating whether the current node has child nodes.
        /// </summary>
        public override bool HasAttributes
        {
            get
            {
                InternalTrace(">" + (_currentnode.Attributes.Count > 0));
                return (_currentnode.Attributes.Count > 0);
            }
        }

        /// <summary>
        /// Gets a value indicating whether the current node has child nodes.
        /// </summary>
        public override bool HasChildren
        {
            get
            {
                InternalTrace(">" + (_currentnode.ChildNodes.Count > 0));
                return (_currentnode.ChildNodes.Count > 0);
            }
        }

        /// <summary>
        /// Gets a value indicating whether the current node is an empty element.
        /// </summary>
        public override bool IsEmptyElement
        {
            get
            {
                InternalTrace(">" + !HasChildren);
                // REVIEW: is this ok?
                return !HasChildren;
            }
        }

        /// <summary>
        /// Gets the name of the current HTML node without the namespace prefix.
        /// </summary>
        public override string LocalName
        {
            get
            {
                if (_attindex != -1)
                {
                    InternalTrace("att>" + _currentnode.Attributes[_attindex].Name);
                    return _nametable.GetOrAdd(_currentnode.Attributes[_attindex].Name);
                }
                InternalTrace("node>" + _currentnode.Name);
                return _nametable.GetOrAdd(_currentnode.Name);
            }
        }

        /// <summary>
        /// Gets the qualified name of the current node.
        /// </summary>
        public override string Name
        {
            get
            {
                InternalTrace(">" + _currentnode.Name);
                return _nametable.GetOrAdd(_currentnode.Name);
            }
        }

        /// <summary>
        /// Gets the namespace URI (as defined in the W3C Namespace Specification) of the current node.
        /// Always returns string.Empty in the case of HtmlNavigator implementation.
        /// </summary>
        public override string NamespaceURI
        {
            get
            {
                InternalTrace(">");
                return _nametable.GetOrAdd(string.Empty);
            }
        }

        /// <summary>
        /// Gets the <see cref="XmlNameTable"/> associated with this implementation.
        /// </summary>
        public override XmlNameTable NameTable
        {
            get
            {
                InternalTrace(null);
                return _nametable;
            }
        }

        /// <summary>
        /// Gets the type of the current node.
        /// </summary>
        public override XPathNodeType NodeType
        {
            get
            {
                switch (_currentnode.NodeType)
                {
                    case HtmlNodeType.Comment:
                        InternalTrace(">" + XPathNodeType.Comment);
                        return XPathNodeType.Comment;

                    case HtmlNodeType.Document:
                        InternalTrace(">" + XPathNodeType.Root);
                        return XPathNodeType.Root;

                    case HtmlNodeType.Text:
                        InternalTrace(">" + XPathNodeType.Text);
                        return XPathNodeType.Text;

                    case HtmlNodeType.Element:
                        {
                            if (_attindex != -1)
                            {
                                InternalTrace(">" + XPathNodeType.Attribute);
                                return XPathNodeType.Attribute;
                            }
                            InternalTrace(">" + XPathNodeType.Element);
                            return XPathNodeType.Element;
                        }

                    default:
                        throw new NotImplementedException("Internal error: Unhandled HtmlNodeType: " +
                                                          _currentnode.NodeType);
                }
            }
        }

        /// <summary>
        /// Gets the prefix associated with the current node.
        /// Always returns string.Empty in the case of HtmlNavigator implementation.
        /// </summary>
        public override string Prefix
        {
            get
            {
                InternalTrace(null);
                return _nametable.GetOrAdd(string.Empty);
            }
        }

        /// <summary>
        /// Gets the text value of the current node.
        /// </summary>
        public override string Value
        {
            get
            {
                InternalTrace("nt=" + _currentnode.NodeType);
                switch (_currentnode.NodeType)
                {
                    case HtmlNodeType.Comment:
                        InternalTrace(">" + ((HtmlCommentNode) _currentnode).Comment);
                        return ((HtmlCommentNode) _currentnode).Comment;

                    case HtmlNodeType.Document:
                        InternalTrace(">");
                        return "";

                    case HtmlNodeType.Text:
                        InternalTrace(">" + ((HtmlTextNode) _currentnode).Text);
                        return ((HtmlTextNode) _currentnode).Text;

                    case HtmlNodeType.Element:
                        {
                            if (_attindex != -1)
                            {
                                InternalTrace(">" + _currentnode.Attributes[_attindex].Value);
                                return _currentnode.Attributes[_attindex].Value;
                            }
                            return _currentnode.InnerText;
                        }

                    default:
                        throw new NotImplementedException("Internal error: Unhandled HtmlNodeType: " +
                                                          _currentnode.NodeType);
                }
            }
        }

        /// <summary>
        /// Gets the xml:lang scope for the current node.
        /// Always returns string.Empty in the case of HtmlNavigator implementation.
        /// </summary>
        public override string XmlLang
        {
            get
            {
                InternalTrace(null);
                return _nametable.GetOrAdd(string.Empty);
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Creates a new HtmlNavigator positioned at the same node as this HtmlNavigator.
        /// </summary>
        /// <returns>A new HtmlNavigator object positioned at the same node as the original HtmlNavigator.</returns>
        public override XPathNavigator Clone()
        {
            InternalTrace(null);
            return new HtmlNodeNavigator(this);
        }

        /// <summary>
        /// Gets the value of the HTML attribute with the specified LocalName and NamespaceURI.
        /// </summary>
        /// <param name="localName">The local name of the HTML attribute.</param>
        /// <param name="namespaceURI">The namespace URI of the attribute. Unsupported with the HtmlNavigator implementation.</param>
        /// <returns>The value of the specified HTML attribute. String.Empty or null if a matching attribute is not found or if the navigator is not positioned on an element node.</returns>
        public override string GetAttribute(string localName, string namespaceURI)
        {
            InternalTrace("localName=" + localName + ", namespaceURI=" + namespaceURI);
            HtmlAttribute att = _currentnode.Attributes[localName];
            if (att == null)
            {
                InternalTrace(">null");
                return null;
            }
            InternalTrace(">" + att.Value);
            return att.Value;
        }

        /// <summary>
        /// Returns the value of the namespace node corresponding to the specified local name.
        /// Always returns string.Empty for the HtmlNavigator implementation.
        /// </summary>
        /// <param name="name">The local name of the namespace node.</param>
        /// <returns>Always returns string.Empty for the HtmlNavigator implementation.</returns>
        public override string GetNamespace(string name)
        {
            InternalTrace("name=" + name);
            return string.Empty;
        }

        /// <summary>
        /// Determines whether the current HtmlNavigator is at the same position as the specified HtmlNavigator.
        /// </summary>
        /// <param name="other">The HtmlNavigator that you want to compare against.</param>
        /// <returns>true if the two navigators have the same position, otherwise, false.</returns>
        public override bool IsSamePosition(XPathNavigator other)
        {
            HtmlNodeNavigator nav = other as HtmlNodeNavigator;
            if (nav == null)
            {
                InternalTrace(">false");
                return false;
            }
            InternalTrace(">" + (nav._currentnode == _currentnode));
            return (nav._currentnode == _currentnode);
        }

        /// <summary>
        /// Moves to the same position as the specified HtmlNavigator.
        /// </summary>
        /// <param name="other">The HtmlNavigator positioned on the node that you want to move to.</param>
        /// <returns>true if successful, otherwise false. If false, the position of the navigator is unchanged.</returns>
        public override bool MoveTo(XPathNavigator other)
        {
            HtmlNodeNavigator nav = other as HtmlNodeNavigator;
            if (nav == null)
            {
                InternalTrace(">false (nav is not an HtmlNodeNavigator)");
                return false;
            }
            InternalTrace("moveto oid=" + nav.GetHashCode()
                          + ", n:" + nav._currentnode.Name
                          + ", a:" + nav._attindex);

            if (nav._doc == _doc)
            {
                _currentnode = nav._currentnode;
                _attindex = nav._attindex;
                InternalTrace(">true");
                return true;
            }
            // we don't know how to handle that
            InternalTrace(">false (???)");
            return false;
        }

        /// <summary>
        /// Moves to the HTML attribute with matching LocalName and NamespaceURI.
        /// </summary>
        /// <param name="localName">The local name of the HTML attribute.</param>
        /// <param name="namespaceURI">The namespace URI of the attribute. Unsupported with the HtmlNavigator implementation.</param>
        /// <returns>true if the HTML attribute is found, otherwise, false. If false, the position of the navigator does not change.</returns>
        public override bool MoveToAttribute(string localName, string namespaceURI)
        {
            InternalTrace("localName=" + localName + ", namespaceURI=" + namespaceURI);
            int index = _currentnode.Attributes.GetAttributeIndex(localName);
            if (index == -1)
            {
                InternalTrace(">false");
                return false;
            }
            _attindex = index;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves to the first sibling of the current node.
        /// </summary>
        /// <returns>true if the navigator is successful moving to the first sibling node, false if there is no first sibling or if the navigator is currently positioned on an attribute node.</returns>
        public override bool MoveToFirst()
        {
            if (_currentnode.ParentNode == null)
            {
                InternalTrace(">false");
                return false;
            }
            if (_currentnode.ParentNode.FirstChild == null)
            {
                InternalTrace(">false");
                return false;
            }
            _currentnode = _currentnode.ParentNode.FirstChild;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves to the first HTML attribute.
        /// </summary>
        /// <returns>true if the navigator is successful moving to the first HTML attribute, otherwise, false.</returns>
        public override bool MoveToFirstAttribute()
        {
            if (!HasAttributes)
            {
                InternalTrace(">false");
                return false;
            }
            _attindex = 0;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves to the first child of the current node.
        /// </summary>
        /// <returns>true if there is a first child node, otherwise false.</returns>
        public override bool MoveToFirstChild()
        {
            if (!_currentnode.HasChildNodes)
            {
                InternalTrace(">false");
                return false;
            }
            _currentnode = _currentnode.ChildNodes[0];
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves the XPathNavigator to the first namespace node of the current element.
        /// Always returns false for the HtmlNavigator implementation.
        /// </summary>
        /// <param name="scope">An XPathNamespaceScope value describing the namespace scope.</param>
        /// <returns>Always returns false for the HtmlNavigator implementation.</returns>
        public override bool MoveToFirstNamespace(XPathNamespaceScope scope)
        {
            InternalTrace(null);
            return false;
        }

        /// <summary>
        /// Moves to the node that has an attribute of type ID whose value matches the specified string.
        /// </summary>
        /// <param name="id">A string representing the ID value of the node to which you want to move. This argument does not need to be atomized.</param>
        /// <returns>true if the move was successful, otherwise false. If false, the position of the navigator is unchanged.</returns>
        public override bool MoveToId(string id)
        {
            InternalTrace("id=" + id);
            HtmlNode node = _doc.GetElementbyId(id);
            if (node == null)
            {
                InternalTrace(">false");
                return false;
            }
            _currentnode = node;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves the XPathNavigator to the namespace node with the specified local name. 
        /// Always returns false for the HtmlNavigator implementation.
        /// </summary>
        /// <param name="name">The local name of the namespace node.</param>
        /// <returns>Always returns false for the HtmlNavigator implementation.</returns>
        public override bool MoveToNamespace(string name)
        {
            InternalTrace("name=" + name);
            return false;
        }

        /// <summary>
        /// Moves to the next sibling of the current node.
        /// </summary>
        /// <returns>true if the navigator is successful moving to the next sibling node, false if there are no more siblings or if the navigator is currently positioned on an attribute node. If false, the position of the navigator is unchanged.</returns>
        public override bool MoveToNext()
        {
            if (_currentnode.NextSibling == null)
            {
                InternalTrace(">false");
                return false;
            }
            InternalTrace("_c=" + _currentnode.CloneNode(false).OuterHtml);
            InternalTrace("_n=" + _currentnode.NextSibling.CloneNode(false).OuterHtml);
            _currentnode = _currentnode.NextSibling;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves to the next HTML attribute.
        /// </summary>
        /// <returns></returns>
        public override bool MoveToNextAttribute()
        {
            InternalTrace(null);
            if (_attindex >= (_currentnode.Attributes.Count - 1))
            {
                InternalTrace(">false");
                return false;
            }
            _attindex++;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves the XPathNavigator to the next namespace node.
        /// Always returns falsefor the HtmlNavigator implementation.
        /// </summary>
        /// <param name="scope">An XPathNamespaceScope value describing the namespace scope.</param>
        /// <returns>Always returns false for the HtmlNavigator implementation.</returns>
        public override bool MoveToNextNamespace(XPathNamespaceScope scope)
        {
            InternalTrace(null);
            return false;
        }

        /// <summary>
        /// Moves to the parent of the current node.
        /// </summary>
        /// <returns>true if there is a parent node, otherwise false.</returns>
        public override bool MoveToParent()
        {
            if (_currentnode.ParentNode == null)
            {
                InternalTrace(">false");
                return false;
            }
            _currentnode = _currentnode.ParentNode;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves to the previous sibling of the current node.
        /// </summary>
        /// <returns>true if the navigator is successful moving to the previous sibling node, false if there is no previous sibling or if the navigator is currently positioned on an attribute node.</returns>
        public override bool MoveToPrevious()
        {
            if (_currentnode.PreviousSibling == null)
            {
                InternalTrace(">false");
                return false;
            }
            _currentnode = _currentnode.PreviousSibling;
            InternalTrace(">true");
            return true;
        }

        /// <summary>
        /// Moves to the root node to which the current node belongs.
        /// </summary>
        public override void MoveToRoot()
        {
            _currentnode = _doc.DocumentNode;
            InternalTrace(null);
        }

        #endregion

        #region Internal Methods

        [Conditional("TRACE")]
        internal void InternalTrace(object traceValue)
        {
            if (!Trace)
            {
                return;
            }
            StackFrame sf = new StackFrame(1, true);
            string name = sf.GetMethod().Name;
            string nodename = _currentnode == null ? "(null)" : _currentnode.Name;
            string nodevalue;
            if (_currentnode == null)
            {
                nodevalue = "(null)";
            }
            else
            {
                switch (_currentnode.NodeType)
                {
                    case HtmlNodeType.Comment:
                        nodevalue = ((HtmlCommentNode) _currentnode).Comment;
                        break;

                    case HtmlNodeType.Document:
                        nodevalue = "";
                        break;

                    case HtmlNodeType.Text:
                        nodevalue = ((HtmlTextNode) _currentnode).Text;
                        break;

                    default:
                        nodevalue = _currentnode.CloneNode(false).OuterHtml;
                        break;
                }
            }
            System.Diagnostics.Trace.WriteLine(string.Format("oid={0},n={1},a={2},v={3},{4}", GetHashCode(), nodename, _attindex, nodevalue, traceValue), "N!" + name);
        }

        #endregion

        #region Private Methods

        private void Reset()
        {
            InternalTrace(null);
            _currentnode = _doc.DocumentNode;
            _attindex = -1;
        }

        #endregion
    }
}