mscorlib(4.0.0.0) API with additions
XmlDocument.cs
1 using System.Collections;
3 using System.IO;
4 using System.Security;
6 using System.Text;
7 using System.Xml.Schema;
8 using System.Xml.XPath;
9 
10 namespace System.Xml
11 {
13  public class XmlDocument : XmlNode
14  {
15  private XmlImplementation implementation;
16 
17  private DomNameTable domNameTable;
18 
19  private XmlLinkedNode lastChild;
20 
21  private XmlNamedNodeMap entities;
22 
23  private Hashtable htElementIdMap;
24 
25  private Hashtable htElementIDAttrDecl;
26 
27  private SchemaInfo schemaInfo;
28 
29  private XmlSchemaSet schemas;
30 
31  private bool reportValidity;
32 
33  private bool actualLoadingStatus;
34 
35  private XmlNodeChangedEventHandler onNodeInsertingDelegate;
36 
37  private XmlNodeChangedEventHandler onNodeInsertedDelegate;
38 
39  private XmlNodeChangedEventHandler onNodeRemovingDelegate;
40 
41  private XmlNodeChangedEventHandler onNodeRemovedDelegate;
42 
43  private XmlNodeChangedEventHandler onNodeChangingDelegate;
44 
45  private XmlNodeChangedEventHandler onNodeChangedDelegate;
46 
47  internal bool fEntRefNodesPresent;
48 
49  internal bool fCDataNodesPresent;
50 
51  private bool preserveWhitespace;
52 
53  private bool isLoading;
54 
55  internal string strDocumentName;
56 
57  internal string strDocumentFragmentName;
58 
59  internal string strCommentName;
60 
61  internal string strTextName;
62 
63  internal string strCDataSectionName;
64 
65  internal string strEntityName;
66 
67  internal string strID;
68 
69  internal string strXmlns;
70 
71  internal string strXml;
72 
73  internal string strSpace;
74 
75  internal string strLang;
76 
77  internal string strEmpty;
78 
79  internal string strNonSignificantWhitespaceName;
80 
81  internal string strSignificantWhitespaceName;
82 
83  internal string strReservedXmlns;
84 
85  internal string strReservedXml;
86 
87  internal string baseURI;
88 
89  private XmlResolver resolver;
90 
91  internal bool bSetResolver;
92 
93  internal object objLock;
94 
95  private XmlAttribute namespaceXml;
96 
97  internal static EmptyEnumerator EmptyEnumerator = new EmptyEnumerator();
98 
99  internal static IXmlSchemaInfo NotKnownSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.NotKnown);
100 
101  internal static IXmlSchemaInfo ValidSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.Valid);
102 
103  internal static IXmlSchemaInfo InvalidSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.Invalid);
104 
105  internal SchemaInfo DtdSchemaInfo
106  {
107  get
108  {
109  return schemaInfo;
110  }
111  set
112  {
113  schemaInfo = value;
114  }
115  }
116 
119  public override XmlNodeType NodeType => XmlNodeType.Document;
120 
123  public override XmlNode ParentNode => null;
124 
127  public virtual XmlDocumentType DocumentType => (XmlDocumentType)FindChild(XmlNodeType.DocumentType);
128 
129  internal virtual XmlDeclaration Declaration
130  {
131  get
132  {
133  if (HasChildNodes)
134  {
135  return FirstChild as XmlDeclaration;
136  }
137  return null;
138  }
139  }
140 
143  public XmlImplementation Implementation => implementation;
144 
147  public override string Name => strDocumentName;
148 
151  public override string LocalName => strDocumentName;
152 
155  public XmlElement DocumentElement => (XmlElement)FindChild(XmlNodeType.Element);
156 
157  internal override bool IsContainer => true;
158 
159  internal override XmlLinkedNode LastNode
160  {
161  get
162  {
163  return lastChild;
164  }
165  set
166  {
167  lastChild = value;
168  }
169  }
170 
173  public override XmlDocument OwnerDocument => null;
174 
177  public XmlSchemaSet Schemas
178  {
179  get
180  {
181  if (schemas == null)
182  {
183  schemas = new XmlSchemaSet(NameTable);
184  }
185  return schemas;
186  }
187  set
188  {
189  schemas = value;
190  }
191  }
192 
193  internal bool CanReportValidity => reportValidity;
194 
195  internal bool HasSetResolver => bSetResolver;
196 
200  public virtual XmlResolver XmlResolver
201  {
202  set
203  {
204  if (value != null)
205  {
206  try
207  {
208  new NamedPermissionSet("FullTrust").Demand();
209  }
210  catch (SecurityException inner)
211  {
212  throw new SecurityException(Res.GetString("Xml_UntrustedCodeSettingResolver"), inner);
213  }
214  }
215  resolver = value;
216  if (!bSetResolver)
217  {
218  bSetResolver = true;
219  }
220  XmlDocumentType documentType = DocumentType;
221  if (documentType != null)
222  {
223  documentType.DtdSchemaInfo = null;
224  }
225  }
226  }
227 
230  public XmlNameTable NameTable => implementation.NameTable;
231 
235  public bool PreserveWhitespace
236  {
237  get
238  {
239  return preserveWhitespace;
240  }
241  set
242  {
243  preserveWhitespace = value;
244  }
245  }
246 
250  public override bool IsReadOnly => false;
251 
252  internal XmlNamedNodeMap Entities
253  {
254  get
255  {
256  if (entities == null)
257  {
258  entities = new XmlNamedNodeMap(this);
259  }
260  return entities;
261  }
262  set
263  {
264  entities = value;
265  }
266  }
267 
268  internal bool IsLoading
269  {
270  get
271  {
272  return isLoading;
273  }
274  set
275  {
276  isLoading = value;
277  }
278  }
279 
280  internal bool ActualLoadingStatus
281  {
282  get
283  {
284  return actualLoadingStatus;
285  }
286  set
287  {
288  actualLoadingStatus = value;
289  }
290  }
291 
292  internal Encoding TextEncoding
293  {
294  get
295  {
296  if (Declaration != null)
297  {
298  string encoding = Declaration.Encoding;
299  if (encoding.Length > 0)
300  {
301  return System.Text.Encoding.GetEncoding(encoding);
302  }
303  }
304  return null;
305  }
306  }
307 
312  public override string InnerText
313  {
314  set
315  {
316  throw new InvalidOperationException(Res.GetString("Xdom_Document_Innertext"));
317  }
318  }
319 
323  public override string InnerXml
324  {
325  get
326  {
327  return base.InnerXml;
328  }
329  set
330  {
331  LoadXml(value);
332  }
333  }
334 
335  internal string Version => Declaration?.Version;
336 
337  internal string Encoding => Declaration?.Encoding;
338 
339  internal string Standalone => Declaration?.Standalone;
340 
343  public override IXmlSchemaInfo SchemaInfo
344  {
345  get
346  {
347  if (reportValidity)
348  {
349  XmlElement documentElement = DocumentElement;
350  if (documentElement != null)
351  {
352  switch (documentElement.SchemaInfo.Validity)
353  {
354  case XmlSchemaValidity.Valid:
355  return ValidSchemaInfo;
356  case XmlSchemaValidity.Invalid:
357  return InvalidSchemaInfo;
358  }
359  }
360  }
361  return NotKnownSchemaInfo;
362  }
363  }
364 
367  public override string BaseURI => baseURI;
368 
369  internal override XPathNodeType XPNodeType => XPathNodeType.Root;
370 
371  internal bool HasEntityReferences => fEntRefNodesPresent;
372 
373  internal XmlAttribute NamespaceXml
374  {
375  get
376  {
377  if (namespaceXml == null)
378  {
379  namespaceXml = new XmlAttribute(AddAttrXmlName(strXmlns, strXml, strReservedXmlns, null), this);
380  namespaceXml.Value = strReservedXml;
381  }
382  return namespaceXml;
383  }
384  }
385 
388  {
389  add
390  {
391  onNodeInsertingDelegate = (XmlNodeChangedEventHandler)Delegate.Combine(onNodeInsertingDelegate, value);
392  }
393  remove
394  {
395  onNodeInsertingDelegate = (XmlNodeChangedEventHandler)Delegate.Remove(onNodeInsertingDelegate, value);
396  }
397  }
398 
401  {
402  add
403  {
404  onNodeInsertedDelegate = (XmlNodeChangedEventHandler)Delegate.Combine(onNodeInsertedDelegate, value);
405  }
406  remove
407  {
408  onNodeInsertedDelegate = (XmlNodeChangedEventHandler)Delegate.Remove(onNodeInsertedDelegate, value);
409  }
410  }
411 
414  {
415  add
416  {
417  onNodeRemovingDelegate = (XmlNodeChangedEventHandler)Delegate.Combine(onNodeRemovingDelegate, value);
418  }
419  remove
420  {
421  onNodeRemovingDelegate = (XmlNodeChangedEventHandler)Delegate.Remove(onNodeRemovingDelegate, value);
422  }
423  }
424 
427  {
428  add
429  {
430  onNodeRemovedDelegate = (XmlNodeChangedEventHandler)Delegate.Combine(onNodeRemovedDelegate, value);
431  }
432  remove
433  {
434  onNodeRemovedDelegate = (XmlNodeChangedEventHandler)Delegate.Remove(onNodeRemovedDelegate, value);
435  }
436  }
437 
440  {
441  add
442  {
443  onNodeChangingDelegate = (XmlNodeChangedEventHandler)Delegate.Combine(onNodeChangingDelegate, value);
444  }
445  remove
446  {
447  onNodeChangingDelegate = (XmlNodeChangedEventHandler)Delegate.Remove(onNodeChangingDelegate, value);
448  }
449  }
450 
453  {
454  add
455  {
456  onNodeChangedDelegate = (XmlNodeChangedEventHandler)Delegate.Combine(onNodeChangedDelegate, value);
457  }
458  remove
459  {
460  onNodeChangedDelegate = (XmlNodeChangedEventHandler)Delegate.Remove(onNodeChangedDelegate, value);
461  }
462  }
463 
465  public XmlDocument()
466  : this(new XmlImplementation())
467  {
468  }
469 
473  : this(new XmlImplementation(nt))
474  {
475  }
476 
479  protected internal XmlDocument(XmlImplementation imp)
480  {
481  implementation = imp;
482  domNameTable = new DomNameTable(this);
483  XmlNameTable nameTable = NameTable;
484  nameTable.Add(string.Empty);
485  strDocumentName = nameTable.Add("#document");
486  strDocumentFragmentName = nameTable.Add("#document-fragment");
487  strCommentName = nameTable.Add("#comment");
488  strTextName = nameTable.Add("#text");
489  strCDataSectionName = nameTable.Add("#cdata-section");
490  strEntityName = nameTable.Add("#entity");
491  strID = nameTable.Add("id");
492  strNonSignificantWhitespaceName = nameTable.Add("#whitespace");
493  strSignificantWhitespaceName = nameTable.Add("#significant-whitespace");
494  strXmlns = nameTable.Add("xmlns");
495  strXml = nameTable.Add("xml");
496  strSpace = nameTable.Add("space");
497  strLang = nameTable.Add("lang");
498  strReservedXmlns = nameTable.Add("http://www.w3.org/2000/xmlns/");
499  strReservedXml = nameTable.Add("http://www.w3.org/XML/1998/namespace");
500  strEmpty = nameTable.Add(string.Empty);
501  baseURI = string.Empty;
502  objLock = new object();
503  }
504 
505  internal static void CheckName(string name)
506  {
507  int num = ValidateNames.ParseNmtoken(name, 0);
508  if (num < name.Length)
509  {
510  throw new XmlException("Xml_BadNameChar", XmlException.BuildCharExceptionArgs(name, num));
511  }
512  }
513 
514  internal XmlName AddXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo)
515  {
516  return domNameTable.AddName(prefix, localName, namespaceURI, schemaInfo);
517  }
518 
519  internal XmlName GetXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo)
520  {
521  return domNameTable.GetName(prefix, localName, namespaceURI, schemaInfo);
522  }
523 
524  internal XmlName AddAttrXmlName(string prefix, string localName, string namespaceURI, IXmlSchemaInfo schemaInfo)
525  {
526  XmlName xmlName = AddXmlName(prefix, localName, namespaceURI, schemaInfo);
527  if (!IsLoading)
528  {
529  object prefix2 = xmlName.Prefix;
530  object namespaceURI2 = xmlName.NamespaceURI;
531  object localName2 = xmlName.LocalName;
532  if ((prefix2 == strXmlns || (prefix2 == strEmpty && localName2 == strXmlns)) ^ (namespaceURI2 == strReservedXmlns))
533  {
534  throw new ArgumentException(Res.GetString("Xdom_Attr_Reserved_XmlNS", namespaceURI));
535  }
536  }
537  return xmlName;
538  }
539 
540  internal bool AddIdInfo(XmlName eleName, XmlName attrName)
541  {
542  if (htElementIDAttrDecl == null || htElementIDAttrDecl[eleName] == null)
543  {
544  if (htElementIDAttrDecl == null)
545  {
546  htElementIDAttrDecl = new Hashtable();
547  }
548  htElementIDAttrDecl.Add(eleName, attrName);
549  return true;
550  }
551  return false;
552  }
553 
554  private XmlName GetIDInfoByElement_(XmlName eleName)
555  {
556  XmlName xmlName = GetXmlName(eleName.Prefix, eleName.LocalName, string.Empty, null);
557  if (xmlName != null)
558  {
559  return (XmlName)htElementIDAttrDecl[xmlName];
560  }
561  return null;
562  }
563 
564  internal XmlName GetIDInfoByElement(XmlName eleName)
565  {
566  if (htElementIDAttrDecl == null)
567  {
568  return null;
569  }
570  return GetIDInfoByElement_(eleName);
571  }
572 
573  private WeakReference GetElement(ArrayList elementList, XmlElement elem)
574  {
575  ArrayList arrayList = new ArrayList();
576  foreach (WeakReference element in elementList)
577  {
578  if (!element.IsAlive)
579  {
580  arrayList.Add(element);
581  }
582  else if ((XmlElement)element.Target == elem)
583  {
584  return element;
585  }
586  }
587  foreach (WeakReference item in arrayList)
588  {
589  elementList.Remove(item);
590  }
591  return null;
592  }
593 
594  internal void AddElementWithId(string id, XmlElement elem)
595  {
596  if (htElementIdMap == null || !htElementIdMap.Contains(id))
597  {
598  if (htElementIdMap == null)
599  {
600  htElementIdMap = new Hashtable();
601  }
602  ArrayList arrayList = new ArrayList();
603  arrayList.Add(new WeakReference(elem));
604  htElementIdMap.Add(id, arrayList);
605  }
606  else
607  {
608  ArrayList arrayList2 = (ArrayList)htElementIdMap[id];
609  if (GetElement(arrayList2, elem) == null)
610  {
611  arrayList2.Add(new WeakReference(elem));
612  }
613  }
614  }
615 
616  internal void RemoveElementWithId(string id, XmlElement elem)
617  {
618  if (htElementIdMap == null || !htElementIdMap.Contains(id))
619  {
620  return;
621  }
622  ArrayList arrayList = (ArrayList)htElementIdMap[id];
623  WeakReference element = GetElement(arrayList, elem);
624  if (element != null)
625  {
626  arrayList.Remove(element);
627  if (arrayList.Count == 0)
628  {
629  htElementIdMap.Remove(id);
630  }
631  }
632  }
633 
638  public override XmlNode CloneNode(bool deep)
639  {
640  XmlDocument xmlDocument = Implementation.CreateDocument();
641  xmlDocument.SetBaseURI(baseURI);
642  if (deep)
643  {
644  xmlDocument.ImportChildren(this, xmlDocument, deep);
645  }
646  return xmlDocument;
647  }
648 
649  internal XmlResolver GetResolver()
650  {
651  return resolver;
652  }
653 
654  internal override bool IsValidChildType(XmlNodeType type)
655  {
656  switch (type)
657  {
658  case XmlNodeType.ProcessingInstruction:
659  case XmlNodeType.Comment:
660  case XmlNodeType.Whitespace:
661  case XmlNodeType.SignificantWhitespace:
662  return true;
663  case XmlNodeType.DocumentType:
664  if (DocumentType != null)
665  {
666  throw new InvalidOperationException(Res.GetString("Xdom_DualDocumentTypeNode"));
667  }
668  return true;
669  case XmlNodeType.Element:
670  if (DocumentElement != null)
671  {
672  throw new InvalidOperationException(Res.GetString("Xdom_DualDocumentElementNode"));
673  }
674  return true;
675  case XmlNodeType.XmlDeclaration:
676  if (Declaration != null)
677  {
678  throw new InvalidOperationException(Res.GetString("Xdom_DualDeclarationNode"));
679  }
680  return true;
681  default:
682  return false;
683  }
684  }
685 
686  private bool HasNodeTypeInPrevSiblings(XmlNodeType nt, XmlNode refNode)
687  {
688  if (refNode == null)
689  {
690  return false;
691  }
692  XmlNode xmlNode = null;
693  if (refNode.ParentNode != null)
694  {
695  xmlNode = refNode.ParentNode.FirstChild;
696  }
697  while (xmlNode != null)
698  {
699  if (xmlNode.NodeType == nt)
700  {
701  return true;
702  }
703  if (xmlNode == refNode)
704  {
705  break;
706  }
707  xmlNode = xmlNode.NextSibling;
708  }
709  return false;
710  }
711 
712  private bool HasNodeTypeInNextSiblings(XmlNodeType nt, XmlNode refNode)
713  {
714  for (XmlNode xmlNode = refNode; xmlNode != null; xmlNode = xmlNode.NextSibling)
715  {
716  if (xmlNode.NodeType == nt)
717  {
718  return true;
719  }
720  }
721  return false;
722  }
723 
724  internal override bool CanInsertBefore(XmlNode newChild, XmlNode refChild)
725  {
726  if (refChild == null)
727  {
728  refChild = FirstChild;
729  }
730  if (refChild == null)
731  {
732  return true;
733  }
734  switch (newChild.NodeType)
735  {
736  case XmlNodeType.XmlDeclaration:
737  return refChild == FirstChild;
738  case XmlNodeType.ProcessingInstruction:
739  case XmlNodeType.Comment:
740  return refChild.NodeType != XmlNodeType.XmlDeclaration;
741  case XmlNodeType.DocumentType:
742  if (refChild.NodeType != XmlNodeType.XmlDeclaration)
743  {
744  return !HasNodeTypeInPrevSiblings(XmlNodeType.Element, refChild.PreviousSibling);
745  }
746  break;
747  case XmlNodeType.Element:
748  if (refChild.NodeType != XmlNodeType.XmlDeclaration)
749  {
750  return !HasNodeTypeInNextSiblings(XmlNodeType.DocumentType, refChild);
751  }
752  break;
753  }
754  return false;
755  }
756 
757  internal override bool CanInsertAfter(XmlNode newChild, XmlNode refChild)
758  {
759  if (refChild == null)
760  {
761  refChild = LastChild;
762  }
763  if (refChild == null)
764  {
765  return true;
766  }
767  switch (newChild.NodeType)
768  {
769  case XmlNodeType.ProcessingInstruction:
770  case XmlNodeType.Comment:
771  case XmlNodeType.Whitespace:
772  case XmlNodeType.SignificantWhitespace:
773  return true;
774  case XmlNodeType.DocumentType:
775  return !HasNodeTypeInPrevSiblings(XmlNodeType.Element, refChild);
776  case XmlNodeType.Element:
777  return !HasNodeTypeInNextSiblings(XmlNodeType.DocumentType, refChild.NextSibling);
778  default:
779  return false;
780  }
781  }
782 
786  public XmlAttribute CreateAttribute(string name)
787  {
788  string prefix = string.Empty;
789  string localName = string.Empty;
790  string namespaceURI = string.Empty;
791  XmlNode.SplitName(name, out prefix, out localName);
792  SetDefaultNamespace(prefix, localName, ref namespaceURI);
793  return CreateAttribute(prefix, localName, namespaceURI);
794  }
795 
796  internal void SetDefaultNamespace(string prefix, string localName, ref string namespaceURI)
797  {
798  if (prefix == strXmlns || (prefix.Length == 0 && localName == strXmlns))
799  {
800  namespaceURI = strReservedXmlns;
801  }
802  else if (prefix == strXml)
803  {
804  namespaceURI = strReservedXml;
805  }
806  }
807 
811  public virtual XmlCDataSection CreateCDataSection(string data)
812  {
813  fCDataNodesPresent = true;
814  return new XmlCDataSection(data, this);
815  }
816 
820  public virtual XmlComment CreateComment(string data)
821  {
822  return new XmlComment(data, this);
823  }
824 
831  [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
832  public virtual XmlDocumentType CreateDocumentType(string name, string publicId, string systemId, string internalSubset)
833  {
834  return new XmlDocumentType(name, publicId, systemId, internalSubset, this);
835  }
836 
840  {
841  return new XmlDocumentFragment(this);
842  }
843 
847  public XmlElement CreateElement(string name)
848  {
849  string prefix = string.Empty;
850  string localName = string.Empty;
851  XmlNode.SplitName(name, out prefix, out localName);
852  return CreateElement(prefix, localName, string.Empty);
853  }
854 
855  internal void AddDefaultAttributes(XmlElement elem)
856  {
857  SchemaInfo dtdSchemaInfo = DtdSchemaInfo;
858  SchemaElementDecl schemaElementDecl = GetSchemaElementDecl(elem);
859  if (schemaElementDecl == null || schemaElementDecl.AttDefs == null)
860  {
861  return;
862  }
863  IDictionaryEnumerator dictionaryEnumerator = schemaElementDecl.AttDefs.GetEnumerator();
864  while (dictionaryEnumerator.MoveNext())
865  {
866  SchemaAttDef schemaAttDef = (SchemaAttDef)dictionaryEnumerator.Value;
867  if (schemaAttDef.Presence == SchemaDeclBase.Use.Default || schemaAttDef.Presence == SchemaDeclBase.Use.Fixed)
868  {
869  string empty = string.Empty;
870  string name = schemaAttDef.Name.Name;
871  string attrNamespaceURI = string.Empty;
872  if (dtdSchemaInfo.SchemaType == SchemaType.DTD)
873  {
874  empty = schemaAttDef.Name.Namespace;
875  }
876  else
877  {
878  empty = schemaAttDef.Prefix;
879  attrNamespaceURI = schemaAttDef.Name.Namespace;
880  }
881  XmlAttribute attributeNode = PrepareDefaultAttribute(schemaAttDef, empty, name, attrNamespaceURI);
882  elem.SetAttributeNode(attributeNode);
883  }
884  }
885  }
886 
887  private SchemaElementDecl GetSchemaElementDecl(XmlElement elem)
888  {
889  SchemaInfo dtdSchemaInfo = DtdSchemaInfo;
890  if (dtdSchemaInfo != null)
891  {
892  XmlQualifiedName key = new XmlQualifiedName(elem.LocalName, (dtdSchemaInfo.SchemaType == SchemaType.DTD) ? elem.Prefix : elem.NamespaceURI);
893  if (dtdSchemaInfo.ElementDecls.TryGetValue(key, out SchemaElementDecl value))
894  {
895  return value;
896  }
897  }
898  return null;
899  }
900 
901  private XmlAttribute PrepareDefaultAttribute(SchemaAttDef attdef, string attrPrefix, string attrLocalname, string attrNamespaceURI)
902  {
903  SetDefaultNamespace(attrPrefix, attrLocalname, ref attrNamespaceURI);
904  XmlAttribute xmlAttribute = CreateDefaultAttribute(attrPrefix, attrLocalname, attrNamespaceURI);
905  xmlAttribute.InnerXml = attdef.DefaultValueRaw;
906  (xmlAttribute as XmlUnspecifiedAttribute)?.SetSpecified(f: false);
907  return xmlAttribute;
908  }
909 
914  public virtual XmlEntityReference CreateEntityReference(string name)
915  {
916  return new XmlEntityReference(name, this);
917  }
918 
923  public virtual XmlProcessingInstruction CreateProcessingInstruction(string target, string data)
924  {
925  return new XmlProcessingInstruction(target, data, this);
926  }
927 
934  public virtual XmlDeclaration CreateXmlDeclaration(string version, string encoding, string standalone)
935  {
936  return new XmlDeclaration(version, encoding, standalone, this);
937  }
938 
942  public virtual XmlText CreateTextNode(string text)
943  {
944  return new XmlText(text, this);
945  }
946 
951  {
952  return new XmlSignificantWhitespace(text, this);
953  }
954 
957  public override XPathNavigator CreateNavigator()
958  {
959  return CreateNavigator(this);
960  }
961 
965  protected internal virtual XPathNavigator CreateNavigator(XmlNode node)
966  {
967  switch (node.NodeType)
968  {
969  case XmlNodeType.EntityReference:
970  case XmlNodeType.Entity:
971  case XmlNodeType.DocumentType:
972  case XmlNodeType.Notation:
973  case XmlNodeType.XmlDeclaration:
974  return null;
975  case XmlNodeType.Text:
976  case XmlNodeType.CDATA:
977  case XmlNodeType.SignificantWhitespace:
978  {
979  XmlNode parentNode = node.ParentNode;
980  if (parentNode != null)
981  {
982  do
983  {
984  switch (parentNode.NodeType)
985  {
986  case XmlNodeType.Attribute:
987  return null;
988  case XmlNodeType.EntityReference:
989  goto IL_006a;
990  }
991  break;
992  IL_006a:
993  parentNode = parentNode.ParentNode;
994  }
995  while (parentNode != null);
996  }
997  node = NormalizeText(node);
998  break;
999  }
1000  case XmlNodeType.Whitespace:
1001  {
1002  XmlNode parentNode = node.ParentNode;
1003  if (parentNode != null)
1004  {
1005  do
1006  {
1007  switch (parentNode.NodeType)
1008  {
1009  case XmlNodeType.Attribute:
1010  case XmlNodeType.Document:
1011  return null;
1012  case XmlNodeType.EntityReference:
1013  goto IL_009f;
1014  }
1015  break;
1016  IL_009f:
1017  parentNode = parentNode.ParentNode;
1018  }
1019  while (parentNode != null);
1020  }
1021  node = NormalizeText(node);
1022  break;
1023  }
1024  }
1025  return new DocumentXPathNavigator(this, node);
1026  }
1027 
1028  internal static bool IsTextNode(XmlNodeType nt)
1029  {
1030  switch (nt)
1031  {
1032  case XmlNodeType.Text:
1033  case XmlNodeType.CDATA:
1034  case XmlNodeType.Whitespace:
1035  case XmlNodeType.SignificantWhitespace:
1036  return true;
1037  default:
1038  return false;
1039  }
1040  }
1041 
1042  private XmlNode NormalizeText(XmlNode n)
1043  {
1044  XmlNode xmlNode = null;
1045  while (IsTextNode(n.NodeType))
1046  {
1047  xmlNode = n;
1048  n = n.PreviousSibling;
1049  if (n == null)
1050  {
1051  XmlNode xmlNode2 = xmlNode;
1052  while (xmlNode2.ParentNode != null && xmlNode2.ParentNode.NodeType == XmlNodeType.EntityReference)
1053  {
1054  if (xmlNode2.ParentNode.PreviousSibling != null)
1055  {
1056  n = xmlNode2.ParentNode.PreviousSibling;
1057  break;
1058  }
1059  xmlNode2 = xmlNode2.ParentNode;
1060  if (xmlNode2 == null)
1061  {
1062  break;
1063  }
1064  }
1065  }
1066  if (n == null)
1067  {
1068  break;
1069  }
1070  while (n.NodeType == XmlNodeType.EntityReference)
1071  {
1072  n = n.LastChild;
1073  }
1074  }
1075  return xmlNode;
1076  }
1077 
1081  public virtual XmlWhitespace CreateWhitespace(string text)
1082  {
1083  return new XmlWhitespace(text, this);
1084  }
1085 
1089  public virtual XmlNodeList GetElementsByTagName(string name)
1090  {
1091  return new XmlElementList(this, name);
1092  }
1093 
1098  public XmlAttribute CreateAttribute(string qualifiedName, string namespaceURI)
1099  {
1100  string prefix = string.Empty;
1101  string localName = string.Empty;
1102  XmlNode.SplitName(qualifiedName, out prefix, out localName);
1103  return CreateAttribute(prefix, localName, namespaceURI);
1104  }
1105 
1110  public XmlElement CreateElement(string qualifiedName, string namespaceURI)
1111  {
1112  string prefix = string.Empty;
1113  string localName = string.Empty;
1114  XmlNode.SplitName(qualifiedName, out prefix, out localName);
1115  return CreateElement(prefix, localName, namespaceURI);
1116  }
1117 
1122  public virtual XmlNodeList GetElementsByTagName(string localName, string namespaceURI)
1123  {
1124  return new XmlElementList(this, localName, namespaceURI);
1125  }
1126 
1130  public virtual XmlElement GetElementById(string elementId)
1131  {
1132  if (htElementIdMap != null)
1133  {
1134  ArrayList arrayList = (ArrayList)htElementIdMap[elementId];
1135  if (arrayList != null)
1136  {
1137  foreach (WeakReference item in arrayList)
1138  {
1139  XmlElement xmlElement = (XmlElement)item.Target;
1140  if (xmlElement != null && xmlElement.IsConnected())
1141  {
1142  return xmlElement;
1143  }
1144  }
1145  }
1146  }
1147  return null;
1148  }
1149 
1156  public virtual XmlNode ImportNode(XmlNode node, bool deep)
1157  {
1158  return ImportNodeInternal(node, deep);
1159  }
1160 
1161  private XmlNode ImportNodeInternal(XmlNode node, bool deep)
1162  {
1163  XmlNode xmlNode = null;
1164  if (node == null)
1165  {
1166  throw new InvalidOperationException(Res.GetString("Xdom_Import_NullNode"));
1167  }
1168  switch (node.NodeType)
1169  {
1170  case XmlNodeType.Element:
1171  xmlNode = CreateElement(node.Prefix, node.LocalName, node.NamespaceURI);
1172  ImportAttributes(node, xmlNode);
1173  if (deep)
1174  {
1175  ImportChildren(node, xmlNode, deep);
1176  }
1177  break;
1178  case XmlNodeType.Attribute:
1179  xmlNode = CreateAttribute(node.Prefix, node.LocalName, node.NamespaceURI);
1180  ImportChildren(node, xmlNode, deep: true);
1181  break;
1182  case XmlNodeType.Text:
1183  xmlNode = CreateTextNode(node.Value);
1184  break;
1185  case XmlNodeType.Comment:
1186  xmlNode = CreateComment(node.Value);
1187  break;
1188  case XmlNodeType.ProcessingInstruction:
1189  xmlNode = CreateProcessingInstruction(node.Name, node.Value);
1190  break;
1191  case XmlNodeType.XmlDeclaration:
1192  {
1193  XmlDeclaration xmlDeclaration = (XmlDeclaration)node;
1194  xmlNode = CreateXmlDeclaration(xmlDeclaration.Version, xmlDeclaration.Encoding, xmlDeclaration.Standalone);
1195  break;
1196  }
1197  case XmlNodeType.CDATA:
1198  xmlNode = CreateCDataSection(node.Value);
1199  break;
1200  case XmlNodeType.DocumentType:
1201  {
1202  XmlDocumentType xmlDocumentType = (XmlDocumentType)node;
1203  xmlNode = CreateDocumentType(xmlDocumentType.Name, xmlDocumentType.PublicId, xmlDocumentType.SystemId, xmlDocumentType.InternalSubset);
1204  break;
1205  }
1206  case XmlNodeType.DocumentFragment:
1207  xmlNode = CreateDocumentFragment();
1208  if (deep)
1209  {
1210  ImportChildren(node, xmlNode, deep);
1211  }
1212  break;
1213  case XmlNodeType.EntityReference:
1214  xmlNode = CreateEntityReference(node.Name);
1215  break;
1216  case XmlNodeType.Whitespace:
1217  xmlNode = CreateWhitespace(node.Value);
1218  break;
1219  case XmlNodeType.SignificantWhitespace:
1220  xmlNode = CreateSignificantWhitespace(node.Value);
1221  break;
1222  default:
1223  throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Res.GetString("Xdom_Import"), new object[1]
1224  {
1225  node.NodeType.ToString()
1226  }));
1227  }
1228  return xmlNode;
1229  }
1230 
1231  private void ImportAttributes(XmlNode fromElem, XmlNode toElem)
1232  {
1233  int count = fromElem.Attributes.Count;
1234  for (int i = 0; i < count; i++)
1235  {
1236  if (fromElem.Attributes[i].Specified)
1237  {
1238  toElem.Attributes.SetNamedItem(ImportNodeInternal(fromElem.Attributes[i], deep: true));
1239  }
1240  }
1241  }
1242 
1243  private void ImportChildren(XmlNode fromNode, XmlNode toNode, bool deep)
1244  {
1245  for (XmlNode xmlNode = fromNode.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
1246  {
1247  toNode.AppendChild(ImportNodeInternal(xmlNode, deep));
1248  }
1249  }
1250 
1256  public virtual XmlAttribute CreateAttribute(string prefix, string localName, string namespaceURI)
1257  {
1258  return new XmlAttribute(AddAttrXmlName(prefix, localName, namespaceURI, null), this);
1259  }
1260 
1266  protected internal virtual XmlAttribute CreateDefaultAttribute(string prefix, string localName, string namespaceURI)
1267  {
1268  return new XmlUnspecifiedAttribute(prefix, localName, namespaceURI, this);
1269  }
1270 
1276  public virtual XmlElement CreateElement(string prefix, string localName, string namespaceURI)
1277  {
1278  XmlElement xmlElement = new XmlElement(AddXmlName(prefix, localName, namespaceURI, null), empty: true, this);
1279  if (!IsLoading)
1280  {
1281  AddDefaultAttributes(xmlElement);
1282  }
1283  return xmlElement;
1284  }
1285 
1293  public virtual XmlNode CreateNode(XmlNodeType type, string prefix, string name, string namespaceURI)
1294  {
1295  switch (type)
1296  {
1297  case XmlNodeType.Element:
1298  if (prefix != null)
1299  {
1300  return CreateElement(prefix, name, namespaceURI);
1301  }
1302  return CreateElement(name, namespaceURI);
1303  case XmlNodeType.Attribute:
1304  if (prefix != null)
1305  {
1306  return CreateAttribute(prefix, name, namespaceURI);
1307  }
1308  return CreateAttribute(name, namespaceURI);
1309  case XmlNodeType.Text:
1310  return CreateTextNode(string.Empty);
1311  case XmlNodeType.CDATA:
1312  return CreateCDataSection(string.Empty);
1313  case XmlNodeType.EntityReference:
1314  return CreateEntityReference(name);
1315  case XmlNodeType.ProcessingInstruction:
1316  return CreateProcessingInstruction(name, string.Empty);
1317  case XmlNodeType.XmlDeclaration:
1318  return CreateXmlDeclaration("1.0", null, null);
1319  case XmlNodeType.Comment:
1320  return CreateComment(string.Empty);
1321  case XmlNodeType.DocumentFragment:
1322  return CreateDocumentFragment();
1323  case XmlNodeType.DocumentType:
1324  return CreateDocumentType(name, string.Empty, string.Empty, string.Empty);
1325  case XmlNodeType.Document:
1326  return new XmlDocument();
1327  case XmlNodeType.SignificantWhitespace:
1328  return CreateSignificantWhitespace(string.Empty);
1329  case XmlNodeType.Whitespace:
1330  return CreateWhitespace(string.Empty);
1331  default:
1332  throw new ArgumentException(Res.GetString("Arg_CannotCreateNode", type));
1333  }
1334  }
1335 
1342  public virtual XmlNode CreateNode(string nodeTypeString, string name, string namespaceURI)
1343  {
1344  return CreateNode(ConvertToNodeType(nodeTypeString), name, namespaceURI);
1345  }
1346 
1353  public virtual XmlNode CreateNode(XmlNodeType type, string name, string namespaceURI)
1354  {
1355  return CreateNode(type, null, name, namespaceURI);
1356  }
1357 
1362  [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
1363  public virtual XmlNode ReadNode(XmlReader reader)
1364  {
1365  XmlNode xmlNode = null;
1366  try
1367  {
1368  IsLoading = true;
1369  XmlLoader xmlLoader = new XmlLoader();
1370  return xmlLoader.ReadCurrentNode(this, reader);
1371  }
1372  finally
1373  {
1374  IsLoading = false;
1375  }
1376  }
1377 
1378  internal XmlNodeType ConvertToNodeType(string nodeTypeString)
1379  {
1380  if (nodeTypeString == "element")
1381  {
1382  return XmlNodeType.Element;
1383  }
1384  if (nodeTypeString == "attribute")
1385  {
1386  return XmlNodeType.Attribute;
1387  }
1388  if (nodeTypeString == "text")
1389  {
1390  return XmlNodeType.Text;
1391  }
1392  if (nodeTypeString == "cdatasection")
1393  {
1394  return XmlNodeType.CDATA;
1395  }
1396  if (nodeTypeString == "entityreference")
1397  {
1398  return XmlNodeType.EntityReference;
1399  }
1400  if (nodeTypeString == "entity")
1401  {
1402  return XmlNodeType.Entity;
1403  }
1404  if (nodeTypeString == "processinginstruction")
1405  {
1406  return XmlNodeType.ProcessingInstruction;
1407  }
1408  if (nodeTypeString == "comment")
1409  {
1410  return XmlNodeType.Comment;
1411  }
1412  if (nodeTypeString == "document")
1413  {
1414  return XmlNodeType.Document;
1415  }
1416  if (nodeTypeString == "documenttype")
1417  {
1418  return XmlNodeType.DocumentType;
1419  }
1420  if (nodeTypeString == "documentfragment")
1421  {
1422  return XmlNodeType.DocumentFragment;
1423  }
1424  if (nodeTypeString == "notation")
1425  {
1426  return XmlNodeType.Notation;
1427  }
1428  if (nodeTypeString == "significantwhitespace")
1429  {
1430  return XmlNodeType.SignificantWhitespace;
1431  }
1432  if (nodeTypeString == "whitespace")
1433  {
1434  return XmlNodeType.Whitespace;
1435  }
1436  throw new ArgumentException(Res.GetString("Xdom_Invalid_NT_String", nodeTypeString));
1437  }
1438 
1439  private XmlTextReader SetupReader(XmlTextReader tr)
1440  {
1441  tr.XmlValidatingReaderCompatibilityMode = true;
1442  tr.EntityHandling = EntityHandling.ExpandCharEntities;
1443  if (HasSetResolver)
1444  {
1445  tr.XmlResolver = GetResolver();
1446  }
1447  return tr;
1448  }
1449 
1467  public virtual void Load(string filename)
1468  {
1469  XmlTextReader xmlTextReader = SetupReader(new XmlTextReader(filename, NameTable));
1470  try
1471  {
1472  Load(xmlTextReader);
1473  }
1474  finally
1475  {
1476  xmlTextReader.Close();
1477  }
1478  }
1479 
1483  public virtual void Load(Stream inStream)
1484  {
1485  XmlTextReader xmlTextReader = SetupReader(new XmlTextReader(inStream, NameTable));
1486  try
1487  {
1488  Load(xmlTextReader);
1489  }
1490  finally
1491  {
1492  xmlTextReader.Impl.Close(closeInput: false);
1493  }
1494  }
1495 
1499  public virtual void Load(TextReader txtReader)
1500  {
1501  XmlTextReader xmlTextReader = SetupReader(new XmlTextReader(txtReader, NameTable));
1502  try
1503  {
1504  Load(xmlTextReader);
1505  }
1506  finally
1507  {
1508  xmlTextReader.Impl.Close(closeInput: false);
1509  }
1510  }
1511 
1515  public virtual void Load(XmlReader reader)
1516  {
1517  try
1518  {
1519  IsLoading = true;
1520  actualLoadingStatus = true;
1521  RemoveAll();
1522  fEntRefNodesPresent = false;
1523  fCDataNodesPresent = false;
1524  reportValidity = true;
1525  XmlLoader xmlLoader = new XmlLoader();
1526  xmlLoader.Load(this, reader, preserveWhitespace);
1527  }
1528  finally
1529  {
1530  IsLoading = false;
1531  actualLoadingStatus = false;
1532  reportValidity = true;
1533  }
1534  }
1535 
1539  public virtual void LoadXml(string xml)
1540  {
1541  XmlTextReader xmlTextReader = SetupReader(new XmlTextReader(new StringReader(xml), NameTable));
1542  try
1543  {
1544  Load(xmlTextReader);
1545  }
1546  finally
1547  {
1548  xmlTextReader.Close();
1549  }
1550  }
1551 
1555  public virtual void Save(string filename)
1556  {
1557  if (DocumentElement == null)
1558  {
1559  throw new XmlException("Xml_InvalidXmlDocument", Res.GetString("Xdom_NoRootEle"));
1560  }
1561  XmlDOMTextWriter xmlDOMTextWriter = new XmlDOMTextWriter(filename, TextEncoding);
1562  try
1563  {
1564  if (!preserveWhitespace)
1565  {
1566  xmlDOMTextWriter.Formatting = Formatting.Indented;
1567  }
1568  WriteTo(xmlDOMTextWriter);
1569  xmlDOMTextWriter.Flush();
1570  }
1571  finally
1572  {
1573  xmlDOMTextWriter.Close();
1574  }
1575  }
1576 
1580  public virtual void Save(Stream outStream)
1581  {
1582  XmlDOMTextWriter xmlDOMTextWriter = new XmlDOMTextWriter(outStream, TextEncoding);
1583  if (!preserveWhitespace)
1584  {
1585  xmlDOMTextWriter.Formatting = Formatting.Indented;
1586  }
1587  WriteTo(xmlDOMTextWriter);
1588  xmlDOMTextWriter.Flush();
1589  }
1590 
1594  public virtual void Save(TextWriter writer)
1595  {
1596  XmlDOMTextWriter xmlDOMTextWriter = new XmlDOMTextWriter(writer);
1597  if (!preserveWhitespace)
1598  {
1599  xmlDOMTextWriter.Formatting = Formatting.Indented;
1600  }
1601  Save(xmlDOMTextWriter);
1602  }
1603 
1607  public virtual void Save(XmlWriter w)
1608  {
1609  XmlNode xmlNode = FirstChild;
1610  if (xmlNode == null)
1611  {
1612  return;
1613  }
1614  if (w.WriteState == WriteState.Start)
1615  {
1616  if (xmlNode is XmlDeclaration)
1617  {
1618  if (Standalone.Length == 0)
1619  {
1620  w.WriteStartDocument();
1621  }
1622  else if (Standalone == "yes")
1623  {
1624  w.WriteStartDocument(standalone: true);
1625  }
1626  else if (Standalone == "no")
1627  {
1628  w.WriteStartDocument(standalone: false);
1629  }
1630  xmlNode = xmlNode.NextSibling;
1631  }
1632  else
1633  {
1634  w.WriteStartDocument();
1635  }
1636  }
1637  while (xmlNode != null)
1638  {
1639  xmlNode.WriteTo(w);
1640  xmlNode = xmlNode.NextSibling;
1641  }
1642  w.Flush();
1643  }
1644 
1647  public override void WriteTo(XmlWriter w)
1648  {
1649  WriteContentTo(w);
1650  }
1651 
1654  public override void WriteContentTo(XmlWriter xw)
1655  {
1656  IEnumerator enumerator = GetEnumerator();
1657  try
1658  {
1659  while (enumerator.MoveNext())
1660  {
1661  XmlNode xmlNode = (XmlNode)enumerator.Current;
1662  xmlNode.WriteTo(xw);
1663  }
1664  }
1665  finally
1666  {
1667  IDisposable disposable = enumerator as IDisposable;
1668  if (disposable != null)
1669  {
1670  disposable.Dispose();
1671  }
1672  }
1673  }
1674 
1678  public void Validate(ValidationEventHandler validationEventHandler)
1679  {
1680  Validate(validationEventHandler, this);
1681  }
1682 
1689  public void Validate(ValidationEventHandler validationEventHandler, XmlNode nodeToValidate)
1690  {
1691  if (schemas == null || schemas.Count == 0)
1692  {
1693  throw new InvalidOperationException(Res.GetString("XmlDocument_NoSchemaInfo"));
1694  }
1695  XmlDocument document = nodeToValidate.Document;
1696  if (document != this)
1697  {
1698  throw new ArgumentException(Res.GetString("XmlDocument_NodeNotFromDocument", "nodeToValidate"));
1699  }
1700  if (nodeToValidate == this)
1701  {
1702  reportValidity = false;
1703  }
1704  DocumentSchemaValidator documentSchemaValidator = new DocumentSchemaValidator(this, schemas, validationEventHandler);
1705  documentSchemaValidator.Validate(nodeToValidate);
1706  if (nodeToValidate == this)
1707  {
1708  reportValidity = true;
1709  }
1710  }
1711 
1712  internal override XmlNodeChangedEventArgs GetEventArgs(XmlNode node, XmlNode oldParent, XmlNode newParent, string oldValue, string newValue, XmlNodeChangedAction action)
1713  {
1714  reportValidity = false;
1715  switch (action)
1716  {
1717  case XmlNodeChangedAction.Insert:
1718  if (onNodeInsertingDelegate == null && onNodeInsertedDelegate == null)
1719  {
1720  return null;
1721  }
1722  break;
1723  case XmlNodeChangedAction.Remove:
1724  if (onNodeRemovingDelegate == null && onNodeRemovedDelegate == null)
1725  {
1726  return null;
1727  }
1728  break;
1729  case XmlNodeChangedAction.Change:
1730  if (onNodeChangingDelegate == null && onNodeChangedDelegate == null)
1731  {
1732  return null;
1733  }
1734  break;
1735  }
1736  return new XmlNodeChangedEventArgs(node, oldParent, newParent, oldValue, newValue, action);
1737  }
1738 
1739  internal XmlNodeChangedEventArgs GetInsertEventArgsForLoad(XmlNode node, XmlNode newParent)
1740  {
1741  if (onNodeInsertingDelegate == null && onNodeInsertedDelegate == null)
1742  {
1743  return null;
1744  }
1745  string value = node.Value;
1746  return new XmlNodeChangedEventArgs(node, null, newParent, value, value, XmlNodeChangedAction.Insert);
1747  }
1748 
1749  internal override void BeforeEvent(XmlNodeChangedEventArgs args)
1750  {
1751  if (args == null)
1752  {
1753  return;
1754  }
1755  switch (args.Action)
1756  {
1757  case XmlNodeChangedAction.Insert:
1758  if (onNodeInsertingDelegate != null)
1759  {
1760  onNodeInsertingDelegate(this, args);
1761  }
1762  break;
1763  case XmlNodeChangedAction.Remove:
1764  if (onNodeRemovingDelegate != null)
1765  {
1766  onNodeRemovingDelegate(this, args);
1767  }
1768  break;
1769  case XmlNodeChangedAction.Change:
1770  if (onNodeChangingDelegate != null)
1771  {
1772  onNodeChangingDelegate(this, args);
1773  }
1774  break;
1775  }
1776  }
1777 
1778  internal override void AfterEvent(XmlNodeChangedEventArgs args)
1779  {
1780  if (args == null)
1781  {
1782  return;
1783  }
1784  switch (args.Action)
1785  {
1786  case XmlNodeChangedAction.Insert:
1787  if (onNodeInsertedDelegate != null)
1788  {
1789  onNodeInsertedDelegate(this, args);
1790  }
1791  break;
1792  case XmlNodeChangedAction.Remove:
1793  if (onNodeRemovedDelegate != null)
1794  {
1795  onNodeRemovedDelegate(this, args);
1796  }
1797  break;
1798  case XmlNodeChangedAction.Change:
1799  if (onNodeChangedDelegate != null)
1800  {
1801  onNodeChangedDelegate(this, args);
1802  }
1803  break;
1804  }
1805  }
1806 
1807  internal XmlAttribute GetDefaultAttribute(XmlElement elem, string attrPrefix, string attrLocalname, string attrNamespaceURI)
1808  {
1809  SchemaInfo dtdSchemaInfo = DtdSchemaInfo;
1810  SchemaElementDecl schemaElementDecl = GetSchemaElementDecl(elem);
1811  if (schemaElementDecl != null && schemaElementDecl.AttDefs != null)
1812  {
1813  IDictionaryEnumerator dictionaryEnumerator = schemaElementDecl.AttDefs.GetEnumerator();
1814  while (dictionaryEnumerator.MoveNext())
1815  {
1816  SchemaAttDef schemaAttDef = (SchemaAttDef)dictionaryEnumerator.Value;
1817  if ((schemaAttDef.Presence == SchemaDeclBase.Use.Default || schemaAttDef.Presence == SchemaDeclBase.Use.Fixed) && schemaAttDef.Name.Name == attrLocalname && ((dtdSchemaInfo.SchemaType == SchemaType.DTD && schemaAttDef.Name.Namespace == attrPrefix) || (dtdSchemaInfo.SchemaType != SchemaType.DTD && schemaAttDef.Name.Namespace == attrNamespaceURI)))
1818  {
1819  return PrepareDefaultAttribute(schemaAttDef, attrPrefix, attrLocalname, attrNamespaceURI);
1820  }
1821  }
1822  }
1823  return null;
1824  }
1825 
1826  internal XmlEntity GetEntityNode(string name)
1827  {
1828  if (DocumentType != null)
1829  {
1830  XmlNamedNodeMap xmlNamedNodeMap = DocumentType.Entities;
1831  if (xmlNamedNodeMap != null)
1832  {
1833  return (XmlEntity)xmlNamedNodeMap.GetNamedItem(name);
1834  }
1835  }
1836  return null;
1837  }
1838 
1839  internal void SetBaseURI(string inBaseURI)
1840  {
1841  baseURI = inBaseURI;
1842  }
1843 
1844  internal override XmlNode AppendChildForLoad(XmlNode newChild, XmlDocument doc)
1845  {
1846  if (!IsValidChildType(newChild.NodeType))
1847  {
1848  throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_TypeConflict"));
1849  }
1850  if (!CanInsertAfter(newChild, LastChild))
1851  {
1852  throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_Location"));
1853  }
1854  XmlNodeChangedEventArgs insertEventArgsForLoad = GetInsertEventArgsForLoad(newChild, this);
1855  if (insertEventArgsForLoad != null)
1856  {
1857  BeforeEvent(insertEventArgsForLoad);
1858  }
1859  XmlLinkedNode xmlLinkedNode = (XmlLinkedNode)newChild;
1860  if (lastChild == null)
1861  {
1862  xmlLinkedNode.next = xmlLinkedNode;
1863  }
1864  else
1865  {
1866  xmlLinkedNode.next = lastChild.next;
1867  lastChild.next = xmlLinkedNode;
1868  }
1869  lastChild = xmlLinkedNode;
1870  xmlLinkedNode.SetParentForLoad(this);
1871  if (insertEventArgsForLoad != null)
1872  {
1873  AfterEvent(insertEventArgsForLoad);
1874  }
1875  return xmlLinkedNode;
1876  }
1877  }
1878 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
WriteState
Specifies the state of the T:System.Xml.XmlWriter.
Definition: WriteState.cs:5
The XML declaration (for example, <?xml version='1.0'?> ).
delegate void XmlNodeChangedEventHandler(object sender, XmlNodeChangedEventArgs e)
Represents the method that handles E:System.Xml.XmlDocument.NodeChanged, E:System....
virtual XmlDocumentFragment CreateDocumentFragment()
Creates an T:System.Xml.XmlDocumentFragment.
Definition: XmlDocument.cs:839
Represents an XML document. You can use this class to load, validate, edit, add, and position XML in ...
Definition: XmlDocument.cs:13
virtual void Load(string filename)
Loads the XML document from the specified URL.
abstract string LocalName
Gets the local name of the node, when overridden in a derived class.
Definition: XmlNode.cs:163
virtual XmlDocumentType DocumentType
Gets the node containing the DOCTYPE declaration.
Definition: XmlDocument.cs:127
Represents the XML declaration node <?xml version='1.0'...?>.
override void WriteContentTo(XmlWriter xw)
Saves all the children of the XmlDocument node to the specified T:System.Xml.XmlWriter.
IEnumerator GetEnumerator()
Get an enumerator that iterates through the child nodes in the current node.
Definition: XmlNode.cs:1111
virtual void Save(string filename)
Saves the XML document to the specified file. If the specified file exists, this method overwrites it...
XmlNodeChangedEventHandler NodeChanging
Occurs when the P:System.Xml.XmlNode.Value of a node belonging to this document is about to be change...
Definition: XmlDocument.cs:440
Defines the post-schema-validation infoset of a validated XML node.
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
bool MoveNext()
Advances the enumerator to the next element of the collection.
Represents a CDATA section.
virtual XmlNode ParentNode
Gets the parent of this node (for nodes that can have parents).
Definition: XmlNode.cs:60
Represents an ordered collection of nodes.
Definition: XmlNodeList.cs:7
XmlNameTable NameTable
Gets the T:System.Xml.XmlNameTable associated with this implementation.
Definition: XmlDocument.cs:230
override string BaseURI
Gets the base URI of the current node.
Definition: XmlDocument.cs:367
virtual void Save(Stream outStream)
Saves the XML document to the specified stream.
Represents white space in element content.
Definition: XmlWhitespace.cs:6
virtual void Load(Stream inStream)
Loads the XML document from the specified stream.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
abstract void WriteStartDocument()
When overridden in a derived class, writes the XML declaration with the version "1....
abstract void WriteTo(XmlWriter w)
Saves the current node to the specified T:System.Xml.XmlWriter, when overridden in a derived class.
virtual XmlElement GetElementById(string elementId)
Gets the T:System.Xml.XmlElement with the specified ID.
virtual void LoadXml(string xml)
Loads the XML document from the specified string.
XmlElement CreateElement(string name)
Creates an element with the specified name.
Definition: XmlDocument.cs:847
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
virtual XmlNode LastChild
Gets the last child of the node.
Definition: XmlNode.cs:121
virtual void Load(XmlReader reader)
Loads the XML document from the specified T:System.Xml.XmlReader.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
override XPathNavigator CreateNavigator()
Creates a new T:System.Xml.XPath.XPathNavigator object for navigating this document.
Definition: XmlDocument.cs:957
Definition: __Canon.cs:3
virtual XmlNode ReadNode(XmlReader reader)
Creates an T:System.Xml.XmlNode object based on the information in the T:System.Xml....
virtual XmlDeclaration CreateXmlDeclaration(string version, string encoding, string standalone)
Creates an T:System.Xml.XmlDeclaration node with the specified values.
Definition: XmlDocument.cs:934
XmlNodeChangedEventHandler NodeRemoving
Occurs when a node belonging to this document is about to be removed from the document.
Definition: XmlDocument.cs:414
override string InnerText
Throws an T:System.InvalidOperationException in all cases.
Definition: XmlDocument.cs:313
Resolves external XML resources named by a Uniform Resource Identifier (URI).
Definition: XmlResolver.cs:8
Represents an attribute. Valid and default values for the attribute are defined in a document type de...
Definition: XmlAttribute.cs:7
XmlNodeChangedEventHandler NodeChanged
Occurs when the P:System.Xml.XmlNode.Value of a node belonging to this document has been changed.
Definition: XmlDocument.cs:453
Represents an entity reference node.
override bool IsReadOnly
Gets a value indicating whether the current node is read-only.
Definition: XmlDocument.cs:250
override XmlNode CloneNode(bool deep)
Creates a duplicate of this node.
Definition: XmlDocument.cs:638
int Count
Gets the number of logical XML Schema definition language (XSD) schemas in the T:System....
Definition: XmlSchemaSet.cs:98
XmlAttribute CreateAttribute(string qualifiedName, string namespaceURI)
Creates an T:System.Xml.XmlAttribute with the specified qualified name and P:System....
XmlNodeType
Specifies the type of node.
Definition: XmlNodeType.cs:5
Represents a processing instruction, which XML defines to keep processor-specific information in the ...
virtual XmlNodeList GetElementsByTagName(string name)
Returns an T:System.Xml.XmlNodeList containing a list of all descendant elements that match the speci...
EntityHandling
Specifies how the T:System.Xml.XmlTextReader or T:System.Xml.XmlValidatingReader handle entities.
Represents the document type declaration.
static Delegate Remove(Delegate source, Delegate value)
Removes the last occurrence of the invocation list of a delegate from the invocation list of another ...
Definition: Delegate.cs:287
static Delegate Combine(Delegate a, Delegate b)
Concatenates the invocation lists of two delegates.
Definition: Delegate.cs:202
XmlDocument()
Initializes a new instance of the T:System.Xml.XmlDocument class.
Definition: XmlDocument.cs:465
XPathNodeType
Defines the XPath node types that can be returned from the T:System.Xml.XPath.XPathNavigator class.
Definition: XPathNodeType.cs:4
static Encoding GetEncoding(int codepage)
Returns the encoding associated with the specified code page identifier.
Definition: Encoding.cs:1249
virtual string NamespaceURI
Gets the namespace URI of this node.
Definition: XmlNode.cs:143
virtual void Save(XmlWriter w)
Saves the XML document to the specified T:System.Xml.XmlWriter.
virtual bool HasChildNodes
Gets a value indicating whether this node has any child nodes.
Definition: XmlNode.cs:139
Defines the context for a set of T:System.Xml.XmlDocument objects.
XmlSchemaValidity Validity
Gets the T:System.Xml.Schema.XmlSchemaValidity value of this validated XML node.
Represents a writer that provides a fast, non-cached, forward-only way to generate streams or files t...
Definition: XmlWriter.cs:12
XmlNodeChangedEventHandler NodeRemoved
Occurs when a node belonging to this document has been removed from its parent.
Definition: XmlDocument.cs:427
Represents a weak reference, which references an object while still allowing that object to be reclai...
Represents a reader that provides fast, non-cached, forward-only access to XML data....
Represents a collection of nodes that can be accessed by name or index.
Defines a permission set that has a name and description associated with it. This class cannot be inh...
Provides data for the E:System.Xml.XmlDocument.NodeChanged, E:System.Xml.XmlDocument....
void Validate(ValidationEventHandler validationEventHandler, XmlNode nodeToValidate)
Validates the T:System.Xml.XmlNode object specified against the XML Schema Definition Language (XSD) ...
Represents a reader that provides fast, noncached, forward-only access to XML data....
Definition: XmlReader.cs:15
virtual XmlComment CreateComment(string data)
Creates an T:System.Xml.XmlComment containing the specified data.
Definition: XmlDocument.cs:820
override void WriteTo(XmlWriter w)
Saves the XmlDocument node to the specified T:System.Xml.XmlWriter.
virtual XmlText CreateTextNode(string text)
Creates an T:System.Xml.XmlText with the specified text.
Definition: XmlDocument.cs:942
virtual XmlNode ImportNode(XmlNode node, bool deep)
Imports a node from another document to the current document.
XmlElement DocumentElement
Gets the root T:System.Xml.XmlElement for the document.
Definition: XmlDocument.cs:155
override IXmlSchemaInfo SchemaInfo
Returns the Post-Schema-Validation-Infoset (PSVI) of the node.
Definition: XmlDocument.cs:344
string? Encoding
Gets or sets the encoding level of the XML document.
abstract void Flush()
When overridden in a derived class, flushes whatever is in the buffer to the underlying streams and a...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Implements a single-threaded T:System.Xml.XmlNameTable.
Definition: NameTable.cs:5
virtual void Save(TextWriter writer)
Saves the XML document to the specified T:System.IO.TextWriter.
Represents a collection that can contain many different types of permissions.
virtual XmlAttribute CreateAttribute(string prefix, string localName, string namespaceURI)
Creates an T:System.Xml.XmlAttribute with the specified P:System.Xml.XmlNode.Prefix,...
Represents the post-schema-validation infoset of a validated XML node.
Definition: XmlSchemaInfo.cs:4
bool PreserveWhitespace
Gets or sets a value indicating whether to preserve white space in element content.
Definition: XmlDocument.cs:236
virtual XmlDocumentType CreateDocumentType(string name, string publicId, string systemId, string internalSubset)
Returns a new T:System.Xml.XmlDocumentType object.
Definition: XmlDocument.cs:832
virtual XmlNodeList GetElementsByTagName(string localName, string namespaceURI)
Returns an T:System.Xml.XmlNodeList containing a list of all descendant elements that match the speci...
XmlSchemaValidity
Represents the validity of an XML item validated by the T:System.Xml.Schema.XmlSchemaValidator class.
virtual XmlWhitespace CreateWhitespace(string text)
Creates an T:System.Xml.XmlWhitespace node.
Version(int major, int minor, int build, int revision)
Initializes a new instance of the T:System.Version class with the specified major,...
Definition: Version.cs:179
Gets the node immediately preceding or following this node.
Definition: XmlLinkedNode.cs:4
XmlNodeChangedAction
Specifies the type of node change.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
virtual XmlNode GetNamedItem(string name)
Retrieves an T:System.Xml.XmlNode specified by name.
virtual XmlElement CreateElement(string prefix, string localName, string namespaceURI)
Creates an element with the specified P:System.Xml.XmlNode.Prefix, P:System.Xml.XmlDocument....
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
virtual string Value
Gets or sets the value of the node.
Definition: XmlNode.cs:36
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
Represents an element.
Definition: XmlElement.cs:7
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
XmlAttribute CreateAttribute(string name)
Creates an T:System.Xml.XmlAttribute with the specified P:System.Xml.XmlDocument.Name.
Definition: XmlDocument.cs:786
internal XmlDocument(XmlImplementation imp)
Initializes a new instance of the XmlDocument class with the specified T:System.Xml....
Definition: XmlDocument.cs:479
override void Close()
Changes the P:System.Xml.XmlReader.ReadState to Closed.
Table of atomized string objects.
Definition: XmlNameTable.cs:5
XmlDocument(XmlNameTable nt)
Initializes a new instance of the XmlDocument class with the specified T:System.Xml....
Definition: XmlDocument.cs:472
Represents the text content of an element or attribute.
Definition: XmlText.cs:6
virtual XmlProcessingInstruction CreateProcessingInstruction(string target, string data)
Creates an T:System.Xml.XmlProcessingInstruction with the specified name and data.
Definition: XmlDocument.cs:923
override IXmlSchemaInfo SchemaInfo
Gets the post schema validation infoset that has been assigned to this node as a result of schema val...
Definition: XmlElement.cs:148
override XmlDocument OwnerDocument
Gets the T:System.Xml.XmlDocument to which the current node belongs.
Definition: XmlDocument.cs:173
virtual string Prefix
Gets or sets the namespace prefix of this node.
Definition: XmlNode.cs:150
XmlNodeChangedEventHandler NodeInserted
Occurs when a node belonging to this document has been inserted into another node.
Definition: XmlDocument.cs:401
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
Returns detailed information about the last exception.
Definition: XmlException.cs:12
XmlElement CreateElement(string qualifiedName, string namespaceURI)
Creates an T:System.Xml.XmlElement with the qualified name and P:System.Xml.XmlNode....
virtual XmlNode CreateNode(string nodeTypeString, string name, string namespaceURI)
Creates an T:System.Xml.XmlNode with the specified node type, P:System.Xml.XmlDocument....
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual XmlSignificantWhitespace CreateSignificantWhitespace(string text)
Creates an T:System.Xml.XmlSignificantWhitespace node.
Definition: XmlDocument.cs:950
virtual internal XmlAttribute CreateDefaultAttribute(string prefix, string localName, string namespaceURI)
Creates a default attribute with the specified prefix, local name and namespace URI.
void Validate(ValidationEventHandler validationEventHandler)
Validates the T:System.Xml.XmlDocument against the XML Schema Definition Language (XSD) schemas conta...
virtual XmlAttribute SetAttributeNode(XmlAttribute newAttr)
Adds the specified T:System.Xml.XmlAttribute.
Definition: XmlElement.cs:385
virtual void Load(TextReader txtReader)
Loads the XML document from the specified T:System.IO.TextReader.
virtual void Remove(object obj)
Removes the first occurrence of a specific object from the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2832
virtual bool Contains(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
Definition: Hashtable.cs:972
abstract string Add(char[] array, int offset, int length)
When overridden in a derived class, atomizes the specified string and adds it to the XmlNameTable.
virtual XmlNode CreateNode(XmlNodeType type, string name, string namespaceURI)
Creates an T:System.Xml.XmlNode with the specified T:System.Xml.XmlNodeType, P:System....
virtual XmlEntityReference CreateEntityReference(string name)
Creates an T:System.Xml.XmlEntityReference with the specified name.
Definition: XmlDocument.cs:914
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
virtual XmlNode CreateNode(XmlNodeType type, string prefix, string name, string namespaceURI)
Creates a T:System.Xml.XmlNode with the specified T:System.Xml.XmlNodeType, P:System....
Represents a reader that can read a sequential series of characters.
Definition: TextReader.cs:14
object Value
Gets the value of the current dictionary entry.
virtual XmlNode FirstChild
Gets the first child of the node.
Definition: XmlNode.cs:117
XmlNamedNodeMap Entities
Gets the collection of T:System.Xml.XmlEntity nodes declared in the document type declaration.
Provides a cursor model for navigating and editing XML data.
override string LocalName
Gets the local name of the node.
Definition: XmlDocument.cs:151
Enumerates the elements of a nongeneric dictionary.
override XmlNodeType NodeType
Gets the type of the current node.
Definition: XmlDocument.cs:119
Formatting
Specifies formatting options for the T:System.Xml.XmlTextWriter.
Definition: Formatting.cs:4
Represents the content of an XML comment.
Definition: XmlComment.cs:6
The exception that is thrown when a method call is invalid for the object's current state.
override string InnerXml
Gets or sets the markup representing the children of the current node.
Definition: XmlDocument.cs:324
virtual XmlNode NextSibling
Gets the node immediately following this node.
Definition: XmlNode.cs:95
Encoding()
Initializes a new instance of the T:System.Text.Encoding class.
Definition: Encoding.cs:1053
virtual void RemoveAll()
Removes all the child nodes and/or attributes of the current node.
Definition: XmlNode.cs:1143
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
override string Name
Gets the qualified name of the node.
Definition: XmlDocument.cs:147
override XmlNode ParentNode
Gets the parent node of this node (for nodes that can have parents).
Definition: XmlDocument.cs:123
override string Value
Gets or sets the value of the node.
Definition: XmlAttribute.cs:71
XmlImplementation Implementation
Gets the T:System.Xml.XmlImplementation object for the current document.
Definition: XmlDocument.cs:143
virtual XmlCDataSection CreateCDataSection(string data)
Creates an T:System.Xml.XmlCDataSection containing the specified data.
Definition: XmlDocument.cs:811
void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
XmlSchemaSet Schemas
Gets or sets the T:System.Xml.Schema.XmlSchemaSet object associated with this T:System....
Definition: XmlDocument.cs:178
Implements a T:System.IO.TextReader that reads from a string.
Definition: StringReader.cs:10
abstract WriteState WriteState
When overridden in a derived class, gets the state of the writer.
Definition: XmlWriter.cs:36
The exception that is thrown when a security error is detected.
Represents a single node in the XML document.
Definition: XmlNode.cs:13
abstract string Name
Gets the qualified name of the node, when overridden in a derived class.
Definition: XmlNode.cs:20
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
abstract XmlNodeType NodeType
Gets the type of the current node, when overridden in a derived class.
Definition: XmlNode.cs:53
Represents a lightweight object that is useful for tree insert operations.
virtual XmlDocument CreateDocument()
Creates a new T:System.Xml.XmlDocument.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
XmlNodeChangedEventHandler NodeInserting
Occurs when a node belonging to this document is about to be inserted into another node.
Definition: XmlDocument.cs:388
virtual internal XPathNavigator CreateNavigator(XmlNode node)
Creates an T:System.Xml.XPath.XPathNavigator object for navigating this document positioned on the T:...
Definition: XmlDocument.cs:965
Contains a cache of XML Schema definition language (XSD) schemas.
Definition: XmlSchemaSet.cs:8
Represents white space between markup in a mixed content node or white space within an xml:space= 'pr...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16