Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlDocument.cs « Dom « Xml « System « System.Xml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dff26fa7d6bed7a789fd9a7cd52254e3bbda41de (plain)
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
//------------------------------------------------------------------------------
// <copyright file="XmlDocument.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------

namespace System.Xml
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Xml.Schema;
    using System.Xml.XPath;
    using System.Security;
    using System.Security.Permissions;
    using System.Globalization;
    using System.Runtime.Versioning;

    // Represents an entire document. An XmlDocument contains XML data.
    public class XmlDocument: XmlNode {
        private XmlImplementation implementation;
        private DomNameTable domNameTable; // hash table of XmlName
        private XmlLinkedNode lastChild;
        private XmlNamedNodeMap entities;
        private Hashtable htElementIdMap;
        private Hashtable htElementIDAttrDecl; //key: id; object: the ArrayList of the elements that have the same id (connected or disconnected)
        private SchemaInfo schemaInfo;
        private XmlSchemaSet schemas; // schemas associated with the cache
        private bool reportValidity;
        //This variable represents the actual loading status. Since, IsLoading will
        //be manipulated soemtimes for adding content to EntityReference this variable
        //has been added which would always represent the loading status of document.
        private bool actualLoadingStatus;

        private XmlNodeChangedEventHandler onNodeInsertingDelegate;
        private XmlNodeChangedEventHandler onNodeInsertedDelegate;
        private XmlNodeChangedEventHandler onNodeRemovingDelegate;
        private XmlNodeChangedEventHandler onNodeRemovedDelegate;
        private XmlNodeChangedEventHandler onNodeChangingDelegate;
        private XmlNodeChangedEventHandler onNodeChangedDelegate;

        // false if there are no ent-ref present, true if ent-ref nodes are or were present (i.e. if all ent-ref were removed, the doc will not clear this flag)
        internal bool fEntRefNodesPresent;
        internal bool fCDataNodesPresent;

        private bool preserveWhitespace;
        private bool isLoading;

        // special name strings for
        internal string strDocumentName;
        internal string strDocumentFragmentName;
        internal string strCommentName;
        internal string strTextName;
        internal string strCDataSectionName;
        internal string strEntityName;
        internal string strID;
        internal string strXmlns;
        internal string strXml;
        internal string strSpace;
        internal string strLang;
        internal string strEmpty;

        internal string strNonSignificantWhitespaceName;
        internal string strSignificantWhitespaceName;
        internal string strReservedXmlns;
        internal string strReservedXml;

        internal String baseURI;

        private XmlResolver resolver;
        internal bool       bSetResolver;
        internal object     objLock;

        private XmlAttribute namespaceXml;

        static internal EmptyEnumerator EmptyEnumerator = new EmptyEnumerator();
        static internal IXmlSchemaInfo NotKnownSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.NotKnown);
        static internal IXmlSchemaInfo ValidSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.Valid);
        static internal IXmlSchemaInfo InvalidSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.Invalid);

        // Initializes a new instance of the XmlDocument class.
        public XmlDocument(): this( new XmlImplementation() ) {
        }

        // Initializes a new instance
        // of the XmlDocument class with the specified XmlNameTable.
        public XmlDocument( XmlNameTable nt ) : this( new XmlImplementation( nt ) ) {
        }

        protected internal XmlDocument( XmlImplementation imp ): base() {

            implementation = imp;
            domNameTable = new DomNameTable( this );

            // force the following string instances to be default in the nametable
            XmlNameTable nt = this.NameTable;
            nt.Add( string.Empty );            
            strDocumentName = nt.Add( "#document" );
            strDocumentFragmentName  = nt.Add( "#document-fragment" );
            strCommentName = nt.Add( "#comment" );
            strTextName = nt.Add( "#text" );
            strCDataSectionName = nt.Add( "#cdata-section" );
            strEntityName = nt.Add( "#entity" );
            strID = nt.Add( "id" );
            strNonSignificantWhitespaceName = nt.Add( "#whitespace" );
            strSignificantWhitespaceName = nt.Add( "#significant-whitespace" );
            strXmlns = nt.Add( "xmlns" );
            strXml = nt.Add(  "xml" );
            strSpace = nt.Add(  "space" );
            strLang = nt.Add(  "lang" );
            strReservedXmlns = nt.Add( XmlReservedNs.NsXmlNs );
            strReservedXml = nt.Add( XmlReservedNs.NsXml );
            strEmpty = nt.Add( String.Empty );
            baseURI = String.Empty; 

            objLock = new object();
        }

        internal SchemaInfo DtdSchemaInfo {
            get { return schemaInfo; }
            set { schemaInfo = value; }
        }

        // NOTE: This does not correctly check start name char, but we cannot change it since it would be a breaking change.
        internal static void CheckName( String name ) {
            int endPos = ValidateNames.ParseNmtoken( name, 0 );
            if (endPos < name.Length) {
                throw new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(name, endPos));
            }
        }

        internal XmlName AddXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo) { 
            XmlName n = domNameTable.AddName(prefix, localName, namespaceURI, schemaInfo); 
            Debug.Assert( (prefix == null) ? (n.Prefix.Length == 0) : (prefix == n.Prefix) );
            Debug.Assert( n.LocalName == localName );
            Debug.Assert( (namespaceURI == null) ? (n.NamespaceURI.Length == 0) : (n.NamespaceURI == namespaceURI) );
            return n;
        }

        internal XmlName GetXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo) { 
            XmlName n = domNameTable.GetName(prefix, localName, namespaceURI, schemaInfo); 
            Debug.Assert(n == null || ((prefix == null) ? (n.Prefix.Length == 0) : (prefix == n.Prefix)));
            Debug.Assert(n == null || n.LocalName == localName);
            Debug.Assert(n == null || ((namespaceURI == null) ? (n.NamespaceURI.Length == 0) : (n.NamespaceURI == namespaceURI)));
            return n;
        }

        internal XmlName AddAttrXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo) { 
            XmlName xmlName = AddXmlName(prefix, localName, namespaceURI, schemaInfo); 
            Debug.Assert( (prefix == null) ? (xmlName.Prefix.Length == 0) : (prefix == xmlName.Prefix) );
            Debug.Assert( xmlName.LocalName == localName );
            Debug.Assert( (namespaceURI == null) ? (xmlName.NamespaceURI.Length == 0) : (xmlName.NamespaceURI == namespaceURI) );

            if ( !this.IsLoading ) {
                // Use atomized versions instead of prefix, localName and nsURI
                object oPrefix       = xmlName.Prefix;
                object oNamespaceURI = xmlName.NamespaceURI;
                object oLocalName    = xmlName.LocalName;
                if ( ( oPrefix == (object)strXmlns || ( oPrefix == (object)strEmpty && oLocalName == (object)strXmlns ) ) ^ ( oNamespaceURI == (object)strReservedXmlns ) )
                    throw new ArgumentException( Res.GetString( Res.Xdom_Attr_Reserved_XmlNS, namespaceURI ) );
            }
            return xmlName;
        }

        internal bool AddIdInfo( XmlName eleName, XmlName attrName ) {
            //when XmlLoader call XmlDocument.AddInfo, the element.XmlName and attr.XmlName 
            //have already been replaced with the ones that don't have namespace values (or just
            //string.Empty) because in DTD, the namespace is not supported
            if ( htElementIDAttrDecl == null || htElementIDAttrDecl[eleName] == null ) {
                if ( htElementIDAttrDecl == null )
                    htElementIDAttrDecl = new Hashtable();
                htElementIDAttrDecl.Add(eleName, attrName);
                return true;
            }
            return false;
        }

        private XmlName GetIDInfoByElement_(XmlName eleName)
        {
            //When XmlDocument is getting the IDAttribute for a given element, 
            //we need only compare the prefix and localname of element.XmlName with
            //the registered htElementIDAttrDecl.
            XmlName newName = GetXmlName(eleName.Prefix, eleName.LocalName, string.Empty, null);
            if (newName != null) {
                return (XmlName)(htElementIDAttrDecl[newName]);
            }
            return null;
        }

        internal XmlName GetIDInfoByElement( XmlName eleName ) {
            if (htElementIDAttrDecl == null)
                return null;
            else
                return GetIDInfoByElement_(eleName);
        }

        private WeakReference GetElement(ArrayList elementList, XmlElement elem) {
            ArrayList gcElemRefs = new ArrayList();
            foreach( WeakReference elemRef in elementList) {
                if ( !elemRef.IsAlive)
                    //take notes on the garbage collected nodes
                    gcElemRefs.Add(elemRef);
                else {
                    if ((XmlElement)(elemRef.Target) == elem)
                        return elemRef;
                }
            }                 
            //Clear out the gced elements
            foreach( WeakReference elemRef in gcElemRefs)
                elementList.Remove(elemRef);
            return null;
        }

        internal void AddElementWithId( string id, XmlElement elem ) {
            if (htElementIdMap == null || !htElementIdMap.Contains(id)) {
                if ( htElementIdMap == null )
                    htElementIdMap = new Hashtable();
                ArrayList elementList = new ArrayList();
                elementList.Add(new WeakReference(elem));
                htElementIdMap.Add(id, elementList);
            }
            else {
                // there are other element(s) that has the same id
                ArrayList elementList = (ArrayList)(htElementIdMap[id]);
                if (GetElement(elementList, elem) == null)
                    elementList.Add(new WeakReference(elem));
            }
        }

        internal void RemoveElementWithId( string id, XmlElement elem ) {
            if (htElementIdMap != null && htElementIdMap.Contains(id)) {
                ArrayList elementList = (ArrayList)(htElementIdMap[id]);
                WeakReference elemRef = GetElement(elementList, elem);
                if (elemRef != null) {
                    elementList.Remove(elemRef);
                    if (elementList.Count == 0)
                        htElementIdMap.Remove(id);
                }
            }
        }


        // Creates a duplicate of this node.
        public override XmlNode CloneNode( bool deep ) {
            XmlDocument clone = Implementation.CreateDocument();
            clone.SetBaseURI(this.baseURI);
            if (deep)
                clone.ImportChildren( this, clone, deep );

            return clone;
        }

        // Gets the type of the current node.
        public override XmlNodeType NodeType {
            get { return XmlNodeType.Document; }
        }

        public override XmlNode ParentNode {
            get { return null; }
        }

        // Gets the node for the DOCTYPE declaration.
        public virtual XmlDocumentType DocumentType {
            get { return(XmlDocumentType) FindChild( XmlNodeType.DocumentType ); }
        }

        internal virtual XmlDeclaration Declaration {
            get {
                if ( HasChildNodes ) {
                    XmlDeclaration dec = FirstChild as XmlDeclaration;
                    return dec;
                }
                return null;
            }
        }

        // Gets the XmlImplementation object for this document.
        public XmlImplementation Implementation {
            get { return this.implementation; }
        }

        // Gets the name of the node.
        public override String Name  {
            get { return strDocumentName; }
        }

        // Gets the name of the current node without the namespace prefix.
        public override String LocalName {
            get { return strDocumentName; }
        }

        // Gets the root XmlElement for the document.
        public XmlElement DocumentElement {
            get { return(XmlElement)FindChild(XmlNodeType.Element); }
        }

        internal override bool IsContainer {
            get { return true; }
        }

        internal override XmlLinkedNode LastNode
        {
            get { return lastChild; }
            set { lastChild = value; }
        }

        // Gets the XmlDocument that contains this node.
        public override XmlDocument OwnerDocument
        {
            get { return null; }
        }

        public XmlSchemaSet Schemas {
            get { 
                if (schemas == null) {
                    schemas = new XmlSchemaSet(NameTable);
                }
                return schemas; 
            }

            set { 
                schemas = value; 
            }
        }

        internal bool CanReportValidity {
            get { return reportValidity; }
        }

        internal bool HasSetResolver {
            get { return bSetResolver; }
        }

        internal XmlResolver GetResolver() {
           return resolver;
        }

        public virtual XmlResolver XmlResolver {
            set {
                if ( value != null ) {
                    try {
                        new NamedPermissionSet( "FullTrust" ).Demand();
                    }
                    catch ( SecurityException e ) {
                        throw new SecurityException( Res.GetString( Res.Xml_UntrustedCodeSettingResolver ), e );
                    }
                }   

                resolver = value;
                if ( !bSetResolver )
                    bSetResolver = true;

                XmlDocumentType dtd = this.DocumentType;
                if ( dtd != null ) {
                    dtd.DtdSchemaInfo = null;
                }
            }
        }
        internal override bool IsValidChildType( XmlNodeType type ) {
            switch ( type ) {
                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.Comment:
                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    return true;

                case XmlNodeType.DocumentType:
                    if ( DocumentType != null )
                        throw new InvalidOperationException( Res.GetString(Res.Xdom_DualDocumentTypeNode) );
                    return true;

                case XmlNodeType.Element:
                    if ( DocumentElement != null )
                        throw new InvalidOperationException( Res.GetString(Res.Xdom_DualDocumentElementNode) );
                    return true;

                case XmlNodeType.XmlDeclaration:
                    if ( Declaration != null )
                        throw new InvalidOperationException( Res.GetString(Res.Xdom_DualDeclarationNode) );
                    return true;

                default:
                    return false;
            }
        }
        // the function examines all the siblings before the refNode
        //  if any of the nodes has type equals to "nt", return true; otherwise, return false;
        private bool HasNodeTypeInPrevSiblings( XmlNodeType nt, XmlNode refNode ) {
            if ( refNode == null )
                return false;

            XmlNode node = null;
            if ( refNode.ParentNode != null )
                node = refNode.ParentNode.FirstChild;
            while ( node != null ) {
                if ( node.NodeType == nt )
                    return true;
                if ( node == refNode )
                    break;
                node = node.NextSibling;
            }
            return false;
        }

        // the function examines all the siblings after the refNode
        //  if any of the nodes has the type equals to "nt", return true; otherwise, return false;
        private bool HasNodeTypeInNextSiblings( XmlNodeType nt, XmlNode refNode ) {
            XmlNode node = refNode;
            while ( node != null ) {
                if ( node.NodeType == nt )
                    return true;
                node = node.NextSibling;
            }
            return false;
        }

        internal override bool CanInsertBefore( XmlNode newChild, XmlNode refChild ) {
            if ( refChild == null )
                refChild = FirstChild;

            if ( refChild == null )
                return true;

            switch ( newChild.NodeType ) {
                case XmlNodeType.XmlDeclaration:
                    return ( refChild == FirstChild ); 

                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.Comment:
                    return refChild.NodeType != XmlNodeType.XmlDeclaration;

                case XmlNodeType.DocumentType: {
                    if ( refChild.NodeType != XmlNodeType.XmlDeclaration ) {
                        //if refChild is not the XmlDeclaration node, only need to go through the sibling before and including refChild to
                        //  make sure no Element ( rootElem node ) before the current position
                        return !HasNodeTypeInPrevSiblings( XmlNodeType.Element, refChild.PreviousSibling );
                    }
                }
                break;

                case XmlNodeType.Element: {
                    if ( refChild.NodeType != XmlNodeType.XmlDeclaration ) {
                        //if refChild is not the XmlDeclaration node, only need to go through the siblings after and including the refChild to
                        //  make sure no DocType node and XmlDeclaration node after the current posistion.
                        return !HasNodeTypeInNextSiblings( XmlNodeType.DocumentType, refChild );
                    }
                }
                break;
            }

            return false;
        }

        internal override bool CanInsertAfter( XmlNode newChild, XmlNode refChild ) {
            if ( refChild == null )
                refChild = LastChild;

            if ( refChild == null )
                return true;

            switch ( newChild.NodeType ) {
                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.Comment:
                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    return true;

                case XmlNodeType.DocumentType: {
                    //we will have to go through all the siblings before the refChild just to make sure no Element node ( rootElem )
                    //  before the current position
                    return !HasNodeTypeInPrevSiblings( XmlNodeType.Element, refChild );
                }

                case XmlNodeType.Element: {
                    return !HasNodeTypeInNextSiblings( XmlNodeType.DocumentType, refChild.NextSibling );
                }

            }

            return false;
        }

        // Creates an XmlAttribute with the specified name.
        public XmlAttribute CreateAttribute( String name ) {
            String prefix = String.Empty;
            String localName = String.Empty;
            String namespaceURI = String.Empty;

            SplitName( name, out prefix, out localName );

            SetDefaultNamespace( prefix, localName, ref namespaceURI );

            return CreateAttribute( prefix, localName, namespaceURI );
        }

        internal void SetDefaultNamespace( String prefix, String localName, ref String namespaceURI ) {
            if ( prefix == strXmlns || ( prefix.Length == 0 && localName == strXmlns ) ) {
                namespaceURI = strReservedXmlns;
            } else if ( prefix == strXml ) {
                namespaceURI = strReservedXml;
            }
        }

        // Creates a XmlCDataSection containing the specified data.
        public virtual XmlCDataSection CreateCDataSection( String data ) {
            fCDataNodesPresent = true;
            return new XmlCDataSection( data, this );
        }

        // Creates an XmlComment containing the specified data.
        public virtual XmlComment CreateComment( String data ) {
            return new XmlComment( data, this );
        }

        // Returns a new XmlDocumentType object.
        [PermissionSetAttribute( SecurityAction.InheritanceDemand, Name = "FullTrust" )]
        public virtual XmlDocumentType CreateDocumentType( string name, string publicId, string systemId, string internalSubset ) {
            return new XmlDocumentType( name, publicId, systemId, internalSubset, this );
        }

        // Creates an XmlDocumentFragment.
        public virtual XmlDocumentFragment CreateDocumentFragment() {
            return new XmlDocumentFragment( this );
        }

        // Creates an element with the specified name.
        public XmlElement CreateElement( String name ) {
            string prefix = String.Empty;
            string localName = String.Empty;
            SplitName( name, out prefix, out localName );
            return CreateElement( prefix, localName, string.Empty );
        }


        internal void AddDefaultAttributes( XmlElement elem ) {
            SchemaInfo schInfo = DtdSchemaInfo;
            SchemaElementDecl ed = GetSchemaElementDecl( elem );
            if ( ed != null && ed.AttDefs != null ) {
                IDictionaryEnumerator attrDefs = ed.AttDefs.GetEnumerator();
                while ( attrDefs.MoveNext() ) {
                    SchemaAttDef attdef = (SchemaAttDef)attrDefs.Value;
                    if ( attdef.Presence == SchemaDeclBase.Use.Default ||
                         attdef.Presence == SchemaDeclBase.Use.Fixed ) {
                         //build a default attribute and return
                         string attrPrefix = string.Empty;
                         string attrLocalname = attdef.Name.Name;
                         string attrNamespaceURI = string.Empty;
                         if ( schInfo.SchemaType == SchemaType.DTD )
                            attrPrefix = attdef.Name.Namespace;
                         else {
                            attrPrefix = attdef.Prefix;
                            attrNamespaceURI = attdef.Name.Namespace;
                         }
                         XmlAttribute defattr = PrepareDefaultAttribute( attdef, attrPrefix, attrLocalname, attrNamespaceURI );
                         elem.SetAttributeNode( defattr );
                    }
                }
            }
        }

        private SchemaElementDecl GetSchemaElementDecl( XmlElement elem ) {
            SchemaInfo schInfo = DtdSchemaInfo;
            if ( schInfo != null ) {
                //build XmlQualifiedName used to identify the element schema declaration
                XmlQualifiedName   qname = new XmlQualifiedName( elem.LocalName, schInfo.SchemaType == SchemaType.DTD ? elem.Prefix : elem.NamespaceURI );
                //get the schema info for the element
                SchemaElementDecl elemDecl;
                if ( schInfo.ElementDecls.TryGetValue(qname, out elemDecl) ) {
                    return elemDecl;
                }
            }
            return null;
        }

        //Will be used by AddDeafulatAttributes() and GetDefaultAttribute() methods
        private XmlAttribute PrepareDefaultAttribute( SchemaAttDef attdef, string attrPrefix, string attrLocalname, string attrNamespaceURI ) {
            SetDefaultNamespace( attrPrefix, attrLocalname, ref attrNamespaceURI );
            XmlAttribute defattr = CreateDefaultAttribute( attrPrefix, attrLocalname, attrNamespaceURI );
            //parsing the default value for the default attribute
            defattr.InnerXml = attdef.DefaultValueRaw;
            //during the expansion of the tree, the flag could be set to true, we need to set it back.
            XmlUnspecifiedAttribute unspAttr = defattr as XmlUnspecifiedAttribute;
            if ( unspAttr != null ) {
                unspAttr.SetSpecified( false );
            }
            return defattr;
        }

        // Creates an XmlEntityReference with the specified name.
        public virtual XmlEntityReference CreateEntityReference( String name ) {
            return new XmlEntityReference( name, this );
        }

        // Creates a XmlProcessingInstruction with the specified name
        // and data strings.
        public virtual XmlProcessingInstruction CreateProcessingInstruction( String target, String data ) {
            return new XmlProcessingInstruction( target, data, this );
        }

        // Creates a XmlDeclaration node with the specified values.
        public virtual XmlDeclaration CreateXmlDeclaration( String version, string encoding, string standalone ) {
            return new XmlDeclaration( version, encoding, standalone, this );
        }

        // Creates an XmlText with the specified text.
        public virtual XmlText CreateTextNode( String text ) {
            return new XmlText( text, this );
        }

        // Creates a XmlSignificantWhitespace node.
        public virtual XmlSignificantWhitespace CreateSignificantWhitespace( string text ) {
            return new XmlSignificantWhitespace( text, this );
        }

        public override XPathNavigator CreateNavigator() {
            return CreateNavigator(this);
        }

        internal protected virtual XPathNavigator CreateNavigator(XmlNode node) {
            XmlNodeType nodeType = node.NodeType;
            XmlNode parent;
            XmlNodeType parentType;

            switch (nodeType) {
                case XmlNodeType.EntityReference:
                case XmlNodeType.Entity:
                case XmlNodeType.DocumentType:
                case XmlNodeType.Notation:
                case XmlNodeType.XmlDeclaration:
                    return null;
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                case XmlNodeType.SignificantWhitespace:
                    parent = node.ParentNode;
                    if (parent != null) {
                        do {
                            parentType = parent.NodeType;
                            if (parentType == XmlNodeType.Attribute) {
                                return null;
                            }
                            else if (parentType == XmlNodeType.EntityReference) {
                                parent = parent.ParentNode;
                            }
                            else {
                                break;
                            }
                        }
                        while (parent != null);
                    }
                    node = NormalizeText(node);
                    break;
                case XmlNodeType.Whitespace:
                    parent = node.ParentNode;
                    if (parent != null) {
                        do {
                            parentType = parent.NodeType;
                            if (parentType == XmlNodeType.Document
                                || parentType == XmlNodeType.Attribute) {
                                return null;
                            }
                            else if (parentType == XmlNodeType.EntityReference) {
                                parent = parent.ParentNode;
                            }
                            else {
                                break;
                            }
                        }
                        while (parent != null);
                    }
                    node = NormalizeText(node);
                    break;
                default:
                    break;
            }
            return new DocumentXPathNavigator(this, node);
        }

        internal static bool IsTextNode( XmlNodeType nt ) {
            switch( nt ) {
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    return true;
                default:
                    return false;
            }
        }

        private XmlNode NormalizeText( XmlNode n ) {
            XmlNode retnode = null;
            while( IsTextNode( n.NodeType ) ) {
                retnode = n;
                n = n.PreviousSibling;

                if( n == null ) {
                    XmlNode intnode = retnode;
                    while ( true ) {
                        if  ( intnode.ParentNode != null && intnode.ParentNode.NodeType == XmlNodeType.EntityReference ) {
                            if (intnode.ParentNode.PreviousSibling != null ) {
                                n = intnode.ParentNode.PreviousSibling;
                                break;
                            }
                            else {
                                intnode = intnode.ParentNode;
                                if( intnode == null )
                                    break;
                            }
                        }
                        else
                            break;
                    }
                }

                if( n == null )
                    break;                    
                while( n.NodeType == XmlNodeType.EntityReference ) {                    
                    n = n.LastChild;
                }
            }
            return retnode;
        }

        // Creates a XmlWhitespace node.
        public virtual XmlWhitespace CreateWhitespace( string text ) {
            return new XmlWhitespace( text, this );
        }

        // Returns an XmlNodeList containing
        // a list of all descendant elements that match the specified name.
        public virtual XmlNodeList GetElementsByTagName( String name ) {
            return new XmlElementList( this, name );
        }

        // DOM Level 2

        // Creates an XmlAttribute with the specified LocalName
        // and NamespaceURI.
        public XmlAttribute CreateAttribute( String qualifiedName, String namespaceURI ) {
            string prefix = String.Empty;
            string localName = String.Empty;

            SplitName( qualifiedName, out prefix, out localName );
            return CreateAttribute( prefix, localName, namespaceURI );
        }

        // Creates an XmlElement with the specified LocalName and
        // NamespaceURI.
        public XmlElement CreateElement( String qualifiedName, String namespaceURI ) {
            string prefix = String.Empty;
            string localName = String.Empty;
            SplitName( qualifiedName, out prefix, out localName );
            return CreateElement( prefix, localName, namespaceURI );
        }

        // Returns a XmlNodeList containing
        // a list of all descendant elements that match the specified name.
        public virtual XmlNodeList GetElementsByTagName( String localName, String namespaceURI ) {
            return new XmlElementList( this, localName, namespaceURI );
        }

        // Returns the XmlElement with the specified ID.
        public virtual XmlElement GetElementById( string elementId ) {
            if (htElementIdMap != null) {
                ArrayList elementList = (ArrayList)(htElementIdMap[elementId]);
                if (elementList != null) {
                    foreach (WeakReference elemRef in elementList) {
                        XmlElement elem = (XmlElement)elemRef.Target;
                        if (elem != null
                            && elem.IsConnected())
                            return elem;
                    }
                }
            }
            return null;
        }

        // Imports a node from another document to this document.
        public virtual XmlNode ImportNode( XmlNode node, bool deep ) {
            return ImportNodeInternal( node, deep );
        }

        private XmlNode ImportNodeInternal( XmlNode node, bool deep ) {
            XmlNode newNode = null;

            if ( node == null ) {
                throw new InvalidOperationException(  Res.GetString(Res.Xdom_Import_NullNode) );
            }
            else {
                switch ( node.NodeType ) {
                    case XmlNodeType.Element:
                        newNode = CreateElement( node.Prefix, node.LocalName, node.NamespaceURI );
                        ImportAttributes( node, newNode );
                        if ( deep )
                            ImportChildren( node, newNode, deep );
                        break;

                    case XmlNodeType.Attribute:
                        Debug.Assert( ((XmlAttribute)node).Specified );
                        newNode = CreateAttribute( node.Prefix, node.LocalName, node.NamespaceURI );
                        ImportChildren( node, newNode, true );
                        break;

                    case XmlNodeType.Text:
                        newNode = CreateTextNode( node.Value );
                        break;
                    case XmlNodeType.Comment:
                        newNode = CreateComment( node.Value);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        newNode = CreateProcessingInstruction( node.Name, node.Value );
                        break;
                    case XmlNodeType.XmlDeclaration:
                        XmlDeclaration decl = (XmlDeclaration) node;
                        newNode = CreateXmlDeclaration( decl.Version, decl.Encoding, decl.Standalone );
                        break;
                    case XmlNodeType.CDATA:
                        newNode = CreateCDataSection( node.Value );
                        break;
                    case XmlNodeType.DocumentType:
                        XmlDocumentType docType = (XmlDocumentType)node;
                        newNode = CreateDocumentType( docType.Name, docType.PublicId, docType.SystemId, docType.InternalSubset );
                        break;
                    case XmlNodeType.DocumentFragment:
                        newNode = CreateDocumentFragment();
                        if (deep)
                            ImportChildren( node, newNode, deep );
                        break;

                    case XmlNodeType.EntityReference:
                        newNode = CreateEntityReference( node.Name );
                        // we don't import the children of entity reference because they might result in different
                        // children nodes given different namesapce context in the new document.
                        break;

                    case XmlNodeType.Whitespace:
                        newNode = CreateWhitespace( node.Value );
                        break;

                    case XmlNodeType.SignificantWhitespace:
                        newNode = CreateSignificantWhitespace( node.Value );
                        break;

                    default:
                        throw new InvalidOperationException( String.Format( CultureInfo.InvariantCulture, Res.GetString(Res.Xdom_Import), node.NodeType.ToString() ) );
                }
            }

            return newNode;
        }

        private void ImportAttributes( XmlNode fromElem, XmlNode toElem ) {
            int cAttr = fromElem.Attributes.Count;
            for ( int iAttr = 0; iAttr < cAttr; iAttr++ ) {
                if ( fromElem.Attributes[iAttr].Specified )
                    toElem.Attributes.SetNamedItem( ImportNodeInternal( fromElem.Attributes[iAttr], true ) );
            }
        }

        private void ImportChildren( XmlNode fromNode, XmlNode toNode, bool deep ) {
            Debug.Assert( toNode.NodeType != XmlNodeType.EntityReference );
            for ( XmlNode n = fromNode.FirstChild; n != null; n = n.NextSibling ) {
                toNode.AppendChild( ImportNodeInternal( n, deep ) );
            }
        }

        // Microsoft extensions
         
        // Gets the XmlNameTable associated with this
        // implementation.
        public XmlNameTable NameTable
        {
            get { return implementation.NameTable; }
        }

        // Creates a XmlAttribute with the specified Prefix, LocalName,
        // and NamespaceURI.
        public virtual XmlAttribute CreateAttribute( string prefix, string localName, string namespaceURI ) {
            return new XmlAttribute( AddAttrXmlName( prefix, localName, namespaceURI, null ), this );
        }

        protected internal virtual XmlAttribute CreateDefaultAttribute( string prefix, string localName, string namespaceURI ) {
            return new XmlUnspecifiedAttribute( prefix, localName, namespaceURI, this );
        }

        public virtual XmlElement CreateElement( string prefix, string localName, string namespaceURI) {
            XmlElement elem = new XmlElement( AddXmlName( prefix, localName, namespaceURI, null ), true, this );
            if ( !IsLoading )
                AddDefaultAttributes( elem );
            return elem;
        }

        // Gets or sets a value indicating whether to preserve whitespace.
        public bool PreserveWhitespace {
            get { return preserveWhitespace;}
            set { preserveWhitespace = value;}
        }

        // Gets a value indicating whether the node is read-only.
        public override bool IsReadOnly {
            get { return false;}
        }

        internal XmlNamedNodeMap Entities {
            get {
                if ( entities == null )
                    entities = new XmlNamedNodeMap( this );
                return entities;
            }
            set { entities = value; }
        }

        internal bool IsLoading {
            get { return isLoading;}
            set { isLoading = value; }
        }

        internal bool ActualLoadingStatus{
            get { return actualLoadingStatus;}
            set { actualLoadingStatus = value; }
        }


        // Creates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.
        public virtual XmlNode CreateNode( XmlNodeType type, string prefix, string name, string namespaceURI ) {
            switch (type) {
                case XmlNodeType.Element:
                    if (prefix != null)
                        return CreateElement( prefix, name, namespaceURI );
                    else
                        return CreateElement( name, namespaceURI );

                case XmlNodeType.Attribute:
                    if (prefix != null)
                        return CreateAttribute( prefix, name, namespaceURI );
                    else
                        return CreateAttribute( name, namespaceURI );

                case XmlNodeType.Text:
                    return CreateTextNode( string.Empty );

                case XmlNodeType.CDATA:
                    return CreateCDataSection( string.Empty );

                case XmlNodeType.EntityReference:
                    return CreateEntityReference( name );

                case XmlNodeType.ProcessingInstruction:
                    return CreateProcessingInstruction( name, string.Empty );

                case XmlNodeType.XmlDeclaration:
                    return CreateXmlDeclaration( "1.0", null, null );

                case XmlNodeType.Comment:
                    return CreateComment( string.Empty );

                case XmlNodeType.DocumentFragment:
                    return CreateDocumentFragment();

                case XmlNodeType.DocumentType:
                    return CreateDocumentType( name, string.Empty, string.Empty, string.Empty );

                case XmlNodeType.Document:
                    return new XmlDocument();

                case XmlNodeType.SignificantWhitespace:
                    return CreateSignificantWhitespace( string.Empty );

                case XmlNodeType.Whitespace:
                    return CreateWhitespace( string.Empty );

                default:
                    throw new ArgumentException( Res.GetString( Res.Arg_CannotCreateNode, type ) );
            }
        }

        // Creates an XmlNode with the specified node type, Name, and
        // NamespaceURI.
        public virtual XmlNode CreateNode( string nodeTypeString, string name, string namespaceURI ) {
            return CreateNode( ConvertToNodeType( nodeTypeString ), name, namespaceURI );
        }

        // Creates an XmlNode with the specified XmlNodeType, Name, and
        // NamespaceURI.
        public virtual XmlNode CreateNode( XmlNodeType type, string name, string namespaceURI ) {
            return CreateNode( type, null, name, namespaceURI );
        }

        // Creates an XmlNode object based on the information in the XmlReader.
        // The reader must be positioned on a node or attribute.
        [PermissionSetAttribute( SecurityAction.InheritanceDemand, Name = "FullTrust" )]
        public virtual XmlNode ReadNode( XmlReader reader ) {
            XmlNode node = null;
            try {
                IsLoading = true;
                XmlLoader loader = new XmlLoader();
                node = loader.ReadCurrentNode( this, reader );
            }
            finally {
                IsLoading = false;
            }
            return node;
        }

        internal XmlNodeType ConvertToNodeType( string nodeTypeString ) {
            if ( nodeTypeString == "element" ) {
                return XmlNodeType.Element;
            }
            else if ( nodeTypeString == "attribute" ) {
                return XmlNodeType.Attribute;
            }
            else if ( nodeTypeString == "text" ) {
                return XmlNodeType.Text;
            }
            else if ( nodeTypeString == "cdatasection" ) {
                return XmlNodeType.CDATA;
            }
            else if ( nodeTypeString == "entityreference" ) {
                return XmlNodeType.EntityReference;
            }
            else if ( nodeTypeString == "entity" ) {
                return XmlNodeType.Entity;
            }
            else if ( nodeTypeString == "processinginstruction" ) {
                return XmlNodeType.ProcessingInstruction;
            }
            else if ( nodeTypeString == "comment" ) {
                return XmlNodeType.Comment;
            }
            else if ( nodeTypeString == "document" ) {
                return XmlNodeType.Document;
            }
            else if ( nodeTypeString == "documenttype" ) {
                return XmlNodeType.DocumentType;
            }
            else if ( nodeTypeString == "documentfragment" ) {
                return XmlNodeType.DocumentFragment;
            }
            else if ( nodeTypeString == "notation" ) {
                return XmlNodeType.Notation;
            }
            else if ( nodeTypeString == "significantwhitespace" ) {
                return XmlNodeType.SignificantWhitespace;
            }
            else if ( nodeTypeString == "whitespace" ) {
                return XmlNodeType.Whitespace;
            }
            throw new ArgumentException( Res.GetString( Res.Xdom_Invalid_NT_String, nodeTypeString ) );
        }


        private XmlTextReader SetupReader( XmlTextReader tr ) {
            tr.XmlValidatingReaderCompatibilityMode = true;
            tr.EntityHandling = EntityHandling.ExpandCharEntities;
            if ( this.HasSetResolver )
                tr.XmlResolver = GetResolver();
            return tr;
        }

        // Loads the XML document from the specified URL.
        [ResourceConsumption(ResourceScope.Machine)]
        [ResourceExposure(ResourceScope.Machine)]
        public virtual void Load( string filename ) {
            XmlTextReader reader = SetupReader( new XmlTextReader( filename, NameTable ) );
            try {
                Load( reader );
            }
            finally {
                reader.Close();
            }
        }

        public virtual void Load( Stream inStream ) {
            XmlTextReader reader = SetupReader( new XmlTextReader( inStream, NameTable ) );
            try {
                Load( reader );
            }
            finally {
                reader.Impl.Close( false );
            }
        }

        // Loads the XML document from the specified TextReader.
        public virtual void Load( TextReader txtReader ) {
            XmlTextReader reader = SetupReader( new XmlTextReader( txtReader, NameTable ) );
            try {
                Load( reader );
            }
            finally {
                reader.Impl.Close( false );
            }
        }

        // Loads the XML document from the specified XmlReader.
        public virtual void Load( XmlReader reader ) {
            try {
                IsLoading = true;
                actualLoadingStatus = true;
                RemoveAll();
                fEntRefNodesPresent = false;
                fCDataNodesPresent  = false;
                reportValidity = true;

                XmlLoader loader = new XmlLoader();
                loader.Load( this, reader, preserveWhitespace );
            }
            finally {
                IsLoading = false;
                actualLoadingStatus = false;

                // Ensure the bit is still on after loading a dtd 
                reportValidity = true;
            }
        }

        // Loads the XML document from the specified string.
        public virtual void LoadXml( string xml ) {
            XmlTextReader reader = SetupReader( new XmlTextReader( new StringReader( xml ), NameTable ));
            try {
                Load( reader );
            }
            finally {
                reader.Close();
            }
        }

        //TextEncoding is the one from XmlDeclaration if there is any
        internal Encoding TextEncoding {
            get {
                if ( Declaration != null )
                {
                    string value = Declaration.Encoding;
                    if ( value.Length > 0 ) {
                        return System.Text.Encoding.GetEncoding( value );
                    }
                }
                return null;
            }
        }

        public override string InnerText {
            set {
                throw new InvalidOperationException(Res.GetString(Res.Xdom_Document_Innertext));
            }
        }

        public override string InnerXml {
            get {
                return base.InnerXml;
            }
            set {
                LoadXml( value );
            }
        }

        // Saves the XML document to the specified file.
        //Saves out the to the file with exact content in the XmlDocument.
        [ResourceConsumption(ResourceScope.Machine)]
        [ResourceExposure(ResourceScope.Machine)]
        public virtual void Save( string filename ) {
            if ( DocumentElement == null )
                throw new XmlException( Res.Xml_InvalidXmlDocument, Res.GetString( Res.Xdom_NoRootEle ) );
            XmlDOMTextWriter xw = new XmlDOMTextWriter( filename, TextEncoding );
            try {
                if ( preserveWhitespace == false )
                    xw.Formatting = Formatting.Indented;
                 WriteTo( xw );
                 xw.Flush();
            }
            finally {
                xw.Close();
            }
        }

        //Saves out the to the file with exact content in the XmlDocument.
        public virtual void Save( Stream outStream ) {
            XmlDOMTextWriter xw = new XmlDOMTextWriter( outStream, TextEncoding );
            if ( preserveWhitespace == false )
                xw.Formatting = Formatting.Indented;
            WriteTo( xw );
            xw.Flush();
        }

        // Saves the XML document to the specified TextWriter.
        //
        //Saves out the file with xmldeclaration which has encoding value equal to
        //that of textwriter's encoding
        public virtual void Save( TextWriter writer ) {
            XmlDOMTextWriter xw = new XmlDOMTextWriter( writer );
            if ( preserveWhitespace == false )
                    xw.Formatting = Formatting.Indented;
            Save( xw );
        }

        // Saves the XML document to the specified XmlWriter.
        // 
        //Saves out the file with xmldeclaration which has encoding value equal to
        //that of textwriter's encoding
        public virtual void Save( XmlWriter w ) {
            XmlNode n = this.FirstChild;
            if( n == null )
                return;
            if( w.WriteState == WriteState.Start ) {
                if( n is XmlDeclaration ) {
                    if( Standalone.Length == 0 )
                        w.WriteStartDocument();
                    else if( Standalone == "yes" )
                        w.WriteStartDocument( true );
                    else if( Standalone == "no" )
                        w.WriteStartDocument( false );
                    n = n.NextSibling;
                }
                else {
                    w.WriteStartDocument();
                }
            }
            while( n != null ) {
                //Debug.Assert( n.NodeType != XmlNodeType.XmlDeclaration );
                n.WriteTo( w );
                n = n.NextSibling;
            }
            w.Flush();
        }

        // Saves the node to the specified XmlWriter.
        // 
        //Writes out the to the file with exact content in the XmlDocument.
        public override void WriteTo( XmlWriter w ) {
            WriteContentTo( w );
        }

        // Saves all the children of the node to the specified XmlWriter.
        // 
        //Writes out the to the file with exact content in the XmlDocument.
        public override void WriteContentTo( XmlWriter xw ) {
            foreach( XmlNode n in this ) {
                n.WriteTo( xw );
            }
        }

        public void Validate(ValidationEventHandler validationEventHandler) {
            Validate(validationEventHandler, this);
        }

        public void Validate(ValidationEventHandler validationEventHandler, XmlNode nodeToValidate) {
            if (this.schemas == null || this.schemas.Count == 0) { //Should we error
                throw new InvalidOperationException(Res.GetString(Res.XmlDocument_NoSchemaInfo));
            }
            XmlDocument parentDocument = nodeToValidate.Document;
            if (parentDocument != this) {
                throw new ArgumentException(Res.GetString(Res.XmlDocument_NodeNotFromDocument, "nodeToValidate"));
            }
            if (nodeToValidate == this) {
                reportValidity = false;
            }
            DocumentSchemaValidator validator = new DocumentSchemaValidator(this, schemas, validationEventHandler);
            validator.Validate(nodeToValidate);
            if (nodeToValidate == this) {
                reportValidity = true;
            }
        }

        public event XmlNodeChangedEventHandler NodeInserting {
            add {
                onNodeInsertingDelegate += value;
            }
            remove {
                onNodeInsertingDelegate -= value;
            }
        }

        public event XmlNodeChangedEventHandler NodeInserted {
            add {
                onNodeInsertedDelegate += value;
            }
            remove {
                onNodeInsertedDelegate -= value;
            }
        }

        public event XmlNodeChangedEventHandler NodeRemoving {
            add {
                onNodeRemovingDelegate += value;
            }
            remove {
                onNodeRemovingDelegate -= value;
            }
        }

        public event XmlNodeChangedEventHandler NodeRemoved {
            add {
                onNodeRemovedDelegate += value;
            }
            remove {
                onNodeRemovedDelegate -= value;
            }
        }

        public event XmlNodeChangedEventHandler NodeChanging {
            add {
                onNodeChangingDelegate += value;
            }
            remove {
                onNodeChangingDelegate -= value;
            }
        }

        public event XmlNodeChangedEventHandler NodeChanged {
            add {
                onNodeChangedDelegate += value;
            }
            remove {
                onNodeChangedDelegate -= value;
            }
        }

        internal override XmlNodeChangedEventArgs GetEventArgs(XmlNode node, XmlNode oldParent, XmlNode newParent, string oldValue, string newValue, XmlNodeChangedAction action) {
            reportValidity = false;

            switch (action) {
                case XmlNodeChangedAction.Insert:
                    if (onNodeInsertingDelegate == null && onNodeInsertedDelegate == null) {
                        return null;
                    }
                    break;
                case XmlNodeChangedAction.Remove:
                    if (onNodeRemovingDelegate == null && onNodeRemovedDelegate == null) {
                        return null;
                    }
                    break;
                case XmlNodeChangedAction.Change:
                    if (onNodeChangingDelegate == null && onNodeChangedDelegate == null) {
                        return null;
                    }
                    break;
            }
            return new XmlNodeChangedEventArgs( node, oldParent, newParent, oldValue, newValue, action );
        }

        internal XmlNodeChangedEventArgs GetInsertEventArgsForLoad( XmlNode node, XmlNode newParent ) {
            if (onNodeInsertingDelegate == null && onNodeInsertedDelegate == null) {
                return null;
            }
            string nodeValue = node.Value;
            return new XmlNodeChangedEventArgs(node, null, newParent, nodeValue, nodeValue, XmlNodeChangedAction.Insert);
        }

        internal override void BeforeEvent( XmlNodeChangedEventArgs args ) {
            if ( args != null ) {
                switch ( args.Action ) {
                    case XmlNodeChangedAction.Insert:
                        if ( onNodeInsertingDelegate != null )
                            onNodeInsertingDelegate( this, args );
                        break;

                    case XmlNodeChangedAction.Remove:
                        if ( onNodeRemovingDelegate != null )
                            onNodeRemovingDelegate( this, args );
                        break;

                    case XmlNodeChangedAction.Change:
                        if ( onNodeChangingDelegate != null )
                            onNodeChangingDelegate( this, args );
                        break;
                }
            }
        }

        internal override void AfterEvent( XmlNodeChangedEventArgs args ) {
            if ( args != null ) {
                switch ( args.Action ) {
                    case XmlNodeChangedAction.Insert:
                        if ( onNodeInsertedDelegate != null )
                            onNodeInsertedDelegate( this, args );
                        break;

                    case XmlNodeChangedAction.Remove:
                        if ( onNodeRemovedDelegate != null )
                            onNodeRemovedDelegate( this, args );
                        break;

                    case XmlNodeChangedAction.Change:
                        if ( onNodeChangedDelegate != null )
                            onNodeChangedDelegate( this, args );
                        break;
                }
            }
        }

        // The function such through schema info to find out if there exists a default attribute with passed in names in the passed in element
        // If so, return the newly created default attribute (with children tree);
        // Otherwise, return null.

        internal XmlAttribute GetDefaultAttribute( XmlElement elem, string attrPrefix, string attrLocalname, string attrNamespaceURI ) {
            SchemaInfo schInfo = DtdSchemaInfo;
            SchemaElementDecl ed = GetSchemaElementDecl( elem );
            if ( ed != null && ed.AttDefs != null ) {
                IDictionaryEnumerator attrDefs = ed.AttDefs.GetEnumerator();
                while ( attrDefs.MoveNext() ) {
                    SchemaAttDef attdef = (SchemaAttDef)attrDefs.Value;
                    if ( attdef.Presence == SchemaDeclBase.Use.Default ||
                        attdef.Presence == SchemaDeclBase.Use.Fixed ) {
                        if ( attdef.Name.Name == attrLocalname ) {
                            if ( ( schInfo.SchemaType == SchemaType.DTD && attdef.Name.Namespace == attrPrefix ) ||
                                 ( schInfo.SchemaType != SchemaType.DTD && attdef.Name.Namespace == attrNamespaceURI ) ) {
                                 //find a def attribute with the same name, build a default attribute and return
                                 XmlAttribute defattr = PrepareDefaultAttribute( attdef, attrPrefix, attrLocalname, attrNamespaceURI );
                                 return defattr;
                            }
                        }
                    }
                }
            }
            return null;
        }

        internal String Version {
            get {
                XmlDeclaration decl = Declaration;
                if ( decl != null )
                    return decl.Version;
                return null;
            }
        }

        internal String Encoding {
            get {
                XmlDeclaration decl = Declaration;
                if ( decl != null )
                    return decl.Encoding;
                return null;
            }
        }

        internal String Standalone {
            get {
                XmlDeclaration decl = Declaration;
                if ( decl != null )
                    return decl.Standalone;
                return null;
            }
        }

        internal XmlEntity GetEntityNode( String name ) {
            if ( DocumentType != null ) {
                XmlNamedNodeMap entites = DocumentType.Entities;
                if ( entites != null )
                    return (XmlEntity)(entites.GetNamedItem( name ));
            }
            return null;
        }

        public override IXmlSchemaInfo SchemaInfo {
            get {
                if (reportValidity) {
                    XmlElement documentElement = DocumentElement;
                    if (documentElement != null) {
                        switch (documentElement.SchemaInfo.Validity) {
                            case XmlSchemaValidity.Valid:
                                return ValidSchemaInfo;
                            case XmlSchemaValidity.Invalid:
                                return InvalidSchemaInfo;
                        }
                    }
                }
                return NotKnownSchemaInfo;
            }
        }

        public override String BaseURI {
            get { return baseURI; }
        }

        internal void SetBaseURI( String inBaseURI ) {
            baseURI = inBaseURI;
        }

        internal override XmlNode AppendChildForLoad( XmlNode newChild, XmlDocument doc ) {
            Debug.Assert( doc == this );

            if ( !IsValidChildType( newChild.NodeType ))
                throw new InvalidOperationException( Res.GetString(Res.Xdom_Node_Insert_TypeConflict) );

            if ( !CanInsertAfter( newChild, LastChild ) )
                throw new InvalidOperationException( Res.GetString(Res.Xdom_Node_Insert_Location) );

            XmlNodeChangedEventArgs args = GetInsertEventArgsForLoad( newChild, this );

            if ( args != null )
                BeforeEvent( args );

            XmlLinkedNode newNode = (XmlLinkedNode) newChild;

            if ( lastChild == null ) {
                newNode.next = newNode;
            }
            else {
                newNode.next = lastChild.next;
                lastChild.next = newNode;
            }

            lastChild = newNode;
            newNode.SetParentForLoad( this );

            if ( args != null )
                AfterEvent( args );

            return newNode;
        }

        internal override XPathNodeType XPNodeType { get { return XPathNodeType.Root; } }

        internal bool HasEntityReferences {
            get {
                return fEntRefNodesPresent;
            }
        }

        internal XmlAttribute NamespaceXml {  
            get {
                if (namespaceXml == null) {
                    namespaceXml = new XmlAttribute(AddAttrXmlName(strXmlns, strXml, strReservedXmlns, null), this);
                    namespaceXml.Value = strReservedXml;
                }
                return namespaceXml;
            }
        }
    }
}