mscorlib(4.0.0.0) API with additions
XmlReaderSettings.cs
1 using Microsoft.Win32;
3 using System.IO;
4 using System.Security;
6 using System.Xml.Schema;
8 
9 namespace System.Xml
10 {
12  [global::__DynamicallyInvokable]
13  [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
14  public sealed class XmlReaderSettings
15  {
16  private bool useAsync;
17 
18  private XmlNameTable nameTable;
19 
20  private XmlResolver xmlResolver;
21 
22  private int lineNumberOffset;
23 
24  private int linePositionOffset;
25 
26  private ConformanceLevel conformanceLevel;
27 
28  private bool checkCharacters;
29 
30  private long maxCharactersInDocument;
31 
32  private long maxCharactersFromEntities;
33 
34  private bool ignoreWhitespace;
35 
36  private bool ignorePIs;
37 
38  private bool ignoreComments;
39 
40  private DtdProcessing dtdProcessing;
41 
42  private ValidationType validationType;
43 
44  private XmlSchemaValidationFlags validationFlags;
45 
46  private XmlSchemaSet schemas;
47 
48  private ValidationEventHandler valEventHandler;
49 
50  private bool closeInput;
51 
52  private bool isReadOnly;
53 
54  private static bool? s_enableLegacyXmlSettings;
55 
59  [global::__DynamicallyInvokable]
60  public bool Async
61  {
62  [global::__DynamicallyInvokable]
63  get
64  {
65  return useAsync;
66  }
67  [global::__DynamicallyInvokable]
68  set
69  {
70  CheckReadOnly("Async");
71  useAsync = value;
72  }
73  }
74 
77  [global::__DynamicallyInvokable]
78  public XmlNameTable NameTable
79  {
80  [global::__DynamicallyInvokable]
81  get
82  {
83  return nameTable;
84  }
85  [global::__DynamicallyInvokable]
86  set
87  {
88  CheckReadOnly("NameTable");
89  nameTable = value;
90  }
91  }
92 
93  internal bool IsXmlResolverSet
94  {
95  get;
96  set;
97  }
98 
101  public XmlResolver XmlResolver
102  {
103  set
104  {
105  CheckReadOnly("XmlResolver");
106  xmlResolver = value;
107  IsXmlResolverSet = true;
108  }
109  }
110 
113  [global::__DynamicallyInvokable]
114  public int LineNumberOffset
115  {
116  [global::__DynamicallyInvokable]
117  get
118  {
119  return lineNumberOffset;
120  }
121  [global::__DynamicallyInvokable]
122  set
123  {
124  CheckReadOnly("LineNumberOffset");
125  lineNumberOffset = value;
126  }
127  }
128 
131  [global::__DynamicallyInvokable]
132  public int LinePositionOffset
133  {
134  [global::__DynamicallyInvokable]
135  get
136  {
137  return linePositionOffset;
138  }
139  [global::__DynamicallyInvokable]
140  set
141  {
142  CheckReadOnly("LinePositionOffset");
143  linePositionOffset = value;
144  }
145  }
146 
149  [global::__DynamicallyInvokable]
151  {
152  [global::__DynamicallyInvokable]
153  get
154  {
155  return conformanceLevel;
156  }
157  [global::__DynamicallyInvokable]
158  set
159  {
160  CheckReadOnly("ConformanceLevel");
161  if ((uint)value > 2u)
162  {
163  throw new ArgumentOutOfRangeException("value");
164  }
165  conformanceLevel = value;
166  }
167  }
168 
172  [global::__DynamicallyInvokable]
173  public bool CheckCharacters
174  {
175  [global::__DynamicallyInvokable]
176  get
177  {
178  return checkCharacters;
179  }
180  [global::__DynamicallyInvokable]
181  set
182  {
183  CheckReadOnly("CheckCharacters");
184  checkCharacters = value;
185  }
186  }
187 
190  [global::__DynamicallyInvokable]
191  public long MaxCharactersInDocument
192  {
193  [global::__DynamicallyInvokable]
194  get
195  {
196  return maxCharactersInDocument;
197  }
198  [global::__DynamicallyInvokable]
199  set
200  {
201  CheckReadOnly("MaxCharactersInDocument");
202  if (value < 0)
203  {
204  throw new ArgumentOutOfRangeException("value");
205  }
206  maxCharactersInDocument = value;
207  }
208  }
209 
212  [global::__DynamicallyInvokable]
213  public long MaxCharactersFromEntities
214  {
215  [global::__DynamicallyInvokable]
216  get
217  {
218  return maxCharactersFromEntities;
219  }
220  [global::__DynamicallyInvokable]
221  set
222  {
223  CheckReadOnly("MaxCharactersFromEntities");
224  if (value < 0)
225  {
226  throw new ArgumentOutOfRangeException("value");
227  }
228  maxCharactersFromEntities = value;
229  }
230  }
231 
235  [global::__DynamicallyInvokable]
236  public bool IgnoreWhitespace
237  {
238  [global::__DynamicallyInvokable]
239  get
240  {
241  return ignoreWhitespace;
242  }
243  [global::__DynamicallyInvokable]
244  set
245  {
246  CheckReadOnly("IgnoreWhitespace");
247  ignoreWhitespace = value;
248  }
249  }
250 
254  [global::__DynamicallyInvokable]
255  public bool IgnoreProcessingInstructions
256  {
257  [global::__DynamicallyInvokable]
258  get
259  {
260  return ignorePIs;
261  }
262  [global::__DynamicallyInvokable]
263  set
264  {
265  CheckReadOnly("IgnoreProcessingInstructions");
266  ignorePIs = value;
267  }
268  }
269 
273  [global::__DynamicallyInvokable]
274  public bool IgnoreComments
275  {
276  [global::__DynamicallyInvokable]
277  get
278  {
279  return ignoreComments;
280  }
281  [global::__DynamicallyInvokable]
282  set
283  {
284  CheckReadOnly("IgnoreComments");
285  ignoreComments = value;
286  }
287  }
288 
292  [Obsolete("Use XmlReaderSettings.DtdProcessing property instead.")]
293  public bool ProhibitDtd
294  {
295  get
296  {
297  return dtdProcessing == DtdProcessing.Prohibit;
298  }
299  set
300  {
301  CheckReadOnly("ProhibitDtd");
302  dtdProcessing = ((!value) ? DtdProcessing.Parse : DtdProcessing.Prohibit);
303  }
304  }
305 
308  [global::__DynamicallyInvokable]
310  {
311  [global::__DynamicallyInvokable]
312  get
313  {
314  return dtdProcessing;
315  }
316  [global::__DynamicallyInvokable]
317  set
318  {
319  CheckReadOnly("DtdProcessing");
320  if ((uint)value > 2u)
321  {
322  throw new ArgumentOutOfRangeException("value");
323  }
324  dtdProcessing = value;
325  }
326  }
327 
331  [global::__DynamicallyInvokable]
332  public bool CloseInput
333  {
334  [global::__DynamicallyInvokable]
335  get
336  {
337  return closeInput;
338  }
339  [global::__DynamicallyInvokable]
340  set
341  {
342  CheckReadOnly("CloseInput");
343  closeInput = value;
344  }
345  }
346 
350  {
351  get
352  {
353  return validationType;
354  }
355  set
356  {
357  CheckReadOnly("ValidationType");
358  if ((uint)value > 4u)
359  {
360  throw new ArgumentOutOfRangeException("value");
361  }
362  validationType = value;
363  }
364  }
365 
369  {
370  get
371  {
372  return validationFlags;
373  }
374  set
375  {
376  CheckReadOnly("ValidationFlags");
377  if ((uint)value > 31u)
378  {
379  throw new ArgumentOutOfRangeException("value");
380  }
381  validationFlags = value;
382  }
383  }
384 
387  public XmlSchemaSet Schemas
388  {
389  get
390  {
391  if (schemas == null)
392  {
393  schemas = new XmlSchemaSet();
394  }
395  return schemas;
396  }
397  set
398  {
399  CheckReadOnly("Schemas");
400  schemas = value;
401  }
402  }
403 
404  internal bool ReadOnly
405  {
406  get
407  {
408  return isReadOnly;
409  }
410  set
411  {
412  isReadOnly = value;
413  }
414  }
415 
418  {
419  add
420  {
421  CheckReadOnly("ValidationEventHandler");
422  valEventHandler = (ValidationEventHandler)Delegate.Combine(valEventHandler, value);
423  }
424  remove
425  {
426  CheckReadOnly("ValidationEventHandler");
427  valEventHandler = (ValidationEventHandler)Delegate.Remove(valEventHandler, value);
428  }
429  }
430 
432  [global::__DynamicallyInvokable]
434  {
435  Initialize();
436  }
437 
440  [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.", true)]
441  [EditorBrowsable(EditorBrowsableState.Never)]
443  {
444  Initialize(resolver);
445  }
446 
447  internal XmlResolver GetXmlResolver()
448  {
449  return xmlResolver;
450  }
451 
452  internal XmlResolver GetXmlResolver_CheckConfig()
453  {
454  if (XmlReaderSection.ProhibitDefaultUrlResolver && !IsXmlResolverSet)
455  {
456  return null;
457  }
458  return xmlResolver;
459  }
460 
462  [global::__DynamicallyInvokable]
463  public void Reset()
464  {
465  CheckReadOnly("Reset");
466  Initialize();
467  }
468 
471  [global::__DynamicallyInvokable]
473  {
474  XmlReaderSettings xmlReaderSettings = MemberwiseClone() as XmlReaderSettings;
475  xmlReaderSettings.ReadOnly = false;
476  return xmlReaderSettings;
477  }
478 
479  internal ValidationEventHandler GetEventHandler()
480  {
481  return valEventHandler;
482  }
483 
484  internal XmlReader CreateReader(string inputUri, XmlParserContext inputContext)
485  {
486  if (inputUri == null)
487  {
488  throw new ArgumentNullException("inputUri");
489  }
490  if (inputUri.Length == 0)
491  {
492  throw new ArgumentException(Res.GetString("XmlConvert_BadUri"), "inputUri");
493  }
494  XmlResolver xmlResolver = GetXmlResolver();
495  if (xmlResolver == null)
496  {
497  xmlResolver = CreateDefaultResolver();
498  }
499  XmlReader xmlReader = new XmlTextReaderImpl(inputUri, this, inputContext, xmlResolver);
500  if (ValidationType != 0)
501  {
502  xmlReader = AddValidation(xmlReader);
503  }
504  if (useAsync)
505  {
506  xmlReader = XmlAsyncCheckReader.CreateAsyncCheckWrapper(xmlReader);
507  }
508  return xmlReader;
509  }
510 
511  internal XmlReader CreateReader(Stream input, Uri baseUri, string baseUriString, XmlParserContext inputContext)
512  {
513  if (input == null)
514  {
515  throw new ArgumentNullException("input");
516  }
517  if (baseUriString == null)
518  {
519  baseUriString = ((!(baseUri == null)) ? baseUri.ToString() : string.Empty);
520  }
521  XmlReader xmlReader = new XmlTextReaderImpl(input, null, 0, this, baseUri, baseUriString, inputContext, closeInput);
522  if (ValidationType != 0)
523  {
524  xmlReader = AddValidation(xmlReader);
525  }
526  if (useAsync)
527  {
528  xmlReader = XmlAsyncCheckReader.CreateAsyncCheckWrapper(xmlReader);
529  }
530  return xmlReader;
531  }
532 
533  internal XmlReader CreateReader(TextReader input, string baseUriString, XmlParserContext inputContext)
534  {
535  if (input == null)
536  {
537  throw new ArgumentNullException("input");
538  }
539  if (baseUriString == null)
540  {
541  baseUriString = string.Empty;
542  }
543  XmlReader xmlReader = new XmlTextReaderImpl(input, this, baseUriString, inputContext);
544  if (ValidationType != 0)
545  {
546  xmlReader = AddValidation(xmlReader);
547  }
548  if (useAsync)
549  {
550  xmlReader = XmlAsyncCheckReader.CreateAsyncCheckWrapper(xmlReader);
551  }
552  return xmlReader;
553  }
554 
555  internal XmlReader CreateReader(XmlReader reader)
556  {
557  if (reader == null)
558  {
559  throw new ArgumentNullException("reader");
560  }
561  return AddValidationAndConformanceWrapper(reader);
562  }
563 
564  private void CheckReadOnly(string propertyName)
565  {
566  if (isReadOnly)
567  {
568  throw new XmlException("Xml_ReadOnlyProperty", GetType().Name + "." + propertyName);
569  }
570  }
571 
572  private void Initialize()
573  {
574  Initialize(null);
575  }
576 
577  private void Initialize(XmlResolver resolver)
578  {
579  nameTable = null;
580  if (!EnableLegacyXmlSettings())
581  {
582  xmlResolver = resolver;
583  maxCharactersFromEntities = 10000000L;
584  }
585  else
586  {
587  xmlResolver = ((resolver == null) ? CreateDefaultResolver() : resolver);
588  maxCharactersFromEntities = 0L;
589  }
590  lineNumberOffset = 0;
591  linePositionOffset = 0;
592  checkCharacters = true;
593  conformanceLevel = ConformanceLevel.Document;
594  ignoreWhitespace = false;
595  ignorePIs = false;
596  ignoreComments = false;
597  dtdProcessing = DtdProcessing.Prohibit;
598  closeInput = false;
599  maxCharactersInDocument = 0L;
600  schemas = null;
601  validationType = ValidationType.None;
602  validationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints;
603  validationFlags |= XmlSchemaValidationFlags.AllowXmlAttributes;
604  useAsync = false;
605  isReadOnly = false;
606  IsXmlResolverSet = false;
607  }
608 
609  private static XmlResolver CreateDefaultResolver()
610  {
611  return new XmlUrlResolver();
612  }
613 
614  internal XmlReader AddValidation(XmlReader reader)
615  {
616  if (validationType == ValidationType.Schema)
617  {
618  XmlResolver xmlResolver = GetXmlResolver_CheckConfig();
619  if (xmlResolver == null && !IsXmlResolverSet && !EnableLegacyXmlSettings())
620  {
621  xmlResolver = new XmlUrlResolver();
622  }
623  reader = new XsdValidatingReader(reader, xmlResolver, this);
624  }
625  else if (validationType == ValidationType.DTD)
626  {
627  reader = CreateDtdValidatingReader(reader);
628  }
629  return reader;
630  }
631 
632  private XmlReader AddValidationAndConformanceWrapper(XmlReader reader)
633  {
634  if (validationType == ValidationType.DTD)
635  {
636  reader = CreateDtdValidatingReader(reader);
637  }
638  reader = AddConformanceWrapper(reader);
639  if (validationType == ValidationType.Schema)
640  {
641  reader = new XsdValidatingReader(reader, GetXmlResolver_CheckConfig(), this);
642  }
643  return reader;
644  }
645 
646  private XmlValidatingReaderImpl CreateDtdValidatingReader(XmlReader baseReader)
647  {
648  return new XmlValidatingReaderImpl(baseReader, GetEventHandler(), (ValidationFlags & XmlSchemaValidationFlags.ProcessIdentityConstraints) != XmlSchemaValidationFlags.None);
649  }
650 
651  internal XmlReader AddConformanceWrapper(XmlReader baseReader)
652  {
653  XmlReaderSettings settings = baseReader.Settings;
654  bool flag = false;
655  bool flag2 = false;
656  bool flag3 = false;
657  bool ignorePis = false;
658  DtdProcessing dtdProcessing = (DtdProcessing)(-1);
659  bool flag4 = false;
660  if (settings == null)
661  {
662  if (conformanceLevel != 0 && conformanceLevel != XmlReader.GetV1ConformanceLevel(baseReader))
663  {
664  throw new InvalidOperationException(Res.GetString("Xml_IncompatibleConformanceLevel", conformanceLevel.ToString()));
665  }
666  XmlTextReader xmlTextReader = baseReader as XmlTextReader;
667  if (xmlTextReader == null)
668  {
669  XmlValidatingReader xmlValidatingReader = baseReader as XmlValidatingReader;
670  if (xmlValidatingReader != null)
671  {
672  xmlTextReader = (XmlTextReader)xmlValidatingReader.Reader;
673  }
674  }
675  if (ignoreWhitespace)
676  {
677  WhitespaceHandling whitespaceHandling = WhitespaceHandling.All;
678  if (xmlTextReader != null)
679  {
680  whitespaceHandling = xmlTextReader.WhitespaceHandling;
681  }
682  if (whitespaceHandling == WhitespaceHandling.All)
683  {
684  flag2 = true;
685  flag4 = true;
686  }
687  }
688  if (ignoreComments)
689  {
690  flag3 = true;
691  flag4 = true;
692  }
693  if (ignorePIs)
694  {
695  ignorePis = true;
696  flag4 = true;
697  }
698  DtdProcessing dtdProcessing2 = DtdProcessing.Parse;
699  if (xmlTextReader != null)
700  {
701  dtdProcessing2 = xmlTextReader.DtdProcessing;
702  }
703  if ((this.dtdProcessing == DtdProcessing.Prohibit && dtdProcessing2 != 0) || (this.dtdProcessing == DtdProcessing.Ignore && dtdProcessing2 == DtdProcessing.Parse))
704  {
705  dtdProcessing = this.dtdProcessing;
706  flag4 = true;
707  }
708  }
709  else
710  {
711  if (conformanceLevel != settings.ConformanceLevel && conformanceLevel != 0)
712  {
713  throw new InvalidOperationException(Res.GetString("Xml_IncompatibleConformanceLevel", conformanceLevel.ToString()));
714  }
715  if (checkCharacters && !settings.CheckCharacters)
716  {
717  flag = true;
718  flag4 = true;
719  }
720  if (ignoreWhitespace && !settings.IgnoreWhitespace)
721  {
722  flag2 = true;
723  flag4 = true;
724  }
725  if (ignoreComments && !settings.IgnoreComments)
726  {
727  flag3 = true;
728  flag4 = true;
729  }
730  if (ignorePIs && !settings.IgnoreProcessingInstructions)
731  {
732  ignorePis = true;
733  flag4 = true;
734  }
735  if ((this.dtdProcessing == DtdProcessing.Prohibit && settings.DtdProcessing != 0) || (this.dtdProcessing == DtdProcessing.Ignore && settings.DtdProcessing == DtdProcessing.Parse))
736  {
737  dtdProcessing = this.dtdProcessing;
738  flag4 = true;
739  }
740  }
741  if (flag4)
742  {
743  IXmlNamespaceResolver xmlNamespaceResolver = baseReader as IXmlNamespaceResolver;
744  if (xmlNamespaceResolver != null)
745  {
746  return new XmlCharCheckingReaderWithNS(baseReader, xmlNamespaceResolver, flag, flag2, flag3, ignorePis, dtdProcessing);
747  }
748  return new XmlCharCheckingReader(baseReader, flag, flag2, flag3, ignorePis, dtdProcessing);
749  }
750  return baseReader;
751  }
752 
753  internal static bool EnableLegacyXmlSettings()
754  {
755  if (s_enableLegacyXmlSettings.HasValue)
756  {
757  return s_enableLegacyXmlSettings.Value;
758  }
759  if (!BinaryCompatibility.TargetsAtLeast_Desktop_V4_5_2)
760  {
761  s_enableLegacyXmlSettings = true;
762  return s_enableLegacyXmlSettings.Value;
763  }
764  bool value = false;
765  if (!ReadSettingsFromRegistry(Registry.LocalMachine, ref value))
766  {
767  ReadSettingsFromRegistry(Registry.CurrentUser, ref value);
768  }
769  s_enableLegacyXmlSettings = value;
770  return s_enableLegacyXmlSettings.Value;
771  }
772 
773  [SecuritySafeCritical]
774  [RegistryPermission(SecurityAction.Assert, Unrestricted = true)]
775  private static bool ReadSettingsFromRegistry(RegistryKey hive, ref bool value)
776  {
777  try
778  {
779  using (RegistryKey registryKey = hive.OpenSubKey("SOFTWARE\\Microsoft\\.NETFramework\\XML", writable: false))
780  {
781  if (registryKey != null && registryKey.GetValueKind("EnableLegacyXmlSettings") == RegistryValueKind.DWord)
782  {
783  value = ((int)registryKey.GetValue("EnableLegacyXmlSettings") == 1);
784  return true;
785  }
786  }
787  }
788  catch
789  {
790  }
791  return false;
792  }
793  }
794 }
bool Async
Gets or sets whether asynchronous T:System.Xml.XmlReader methods can be used on a particular T:System...
bool IgnoreProcessingInstructions
Gets or sets a value indicating whether to ignore processing instructions.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
ValidationEventHandler ValidationEventHandler
Occurs when the reader encounters validation errors.
XmlReaderSettings(XmlResolver resolver)
Initializes a new instance of the T:System.Xml.XmlReaderSettings class.
Controls the ability to access registry variables. This class cannot be inherited.
EditorBrowsableState
Specifies the browsable state of a property or method from within an editor.
XmlReaderSettings Clone()
Creates a copy of the T:System.Xml.XmlReaderSettings instance.
XmlReaderSettings()
Initializes a new instance of the T:System.Xml.XmlReaderSettings class.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Resolves external XML resources named by a Uniform Resource Identifier (URI).
Definition: XmlResolver.cs:8
int LineNumberOffset
Gets or sets line number offset of the T:System.Xml.XmlReader object.
ValidationType
Specifies the type of validation to perform.
int LinePositionOffset
Gets or sets line position offset of the T:System.Xml.XmlReader object.
long MaxCharactersInDocument
Gets or sets a value indicating the maximum allowable number of characters in an XML document....
Specifies a set of features to support on the T:System.Xml.XmlReader object created by the Overload:S...
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
DtdProcessing DtdProcessing
Gets or sets a value that determines the processing of DTDs.
bool CheckCharacters
Gets or sets a value indicating whether to do character checking.
XmlSchemaSet Schemas
Gets or sets the T:System.Xml.Schema.XmlSchemaSet to use when performing schema validation.
WhitespaceHandling
Specifies how white space is handled.
ConformanceLevel ConformanceLevel
Gets or sets the level of conformance which the T:System.Xml.XmlReader will comply.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Implements a single-threaded T:System.Xml.XmlNameTable.
Definition: NameTable.cs:5
Represents a collection that can contain many different types of permissions.
ConformanceLevel
Specifies the amount of input or output checking that T:System.Xml.XmlReader and T:System....
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
Table of atomized string objects.
Definition: XmlNameTable.cs:5
XmlSchemaValidationFlags ValidationFlags
Gets or sets a value indicating the schema validation settings. This setting applies to T:System....
bool IgnoreComments
Gets or sets a value indicating whether to ignore comments.
Represents a reader that can read a sequential series of characters.
Definition: TextReader.cs:14
XmlResolver XmlResolver
Sets the T:System.Xml.XmlResolver used to access external documents.
ValidationType ValidationType
Gets or sets a value indicating whether the T:System.Xml.XmlReader will perform validation or type as...
DtdProcessing
Specifies the options for processing DTDs. The T:System.Xml.DtdProcessing enumeration is used by the ...
Definition: DtdProcessing.cs:5
bool? ProhibitDtd
Gets or sets a value indicating whether to prohibit document type definition (DTD) processing....
bool CloseInput
Gets or sets a value indicating whether the underlying stream or T:System.IO.TextReader should be clo...
bool IgnoreWhitespace
Gets or sets a value indicating whether to ignore insignificant white space.
XmlSchemaValidationFlags
Specifies schema validation options used by the T:System.Xml.Schema.XmlSchemaValidator and T:System....
long MaxCharactersFromEntities
Gets or sets a value indicating the maximum allowable number of characters in a document that result ...
void Reset()
Resets the members of the settings class to their default values.
Represents an XML reader section.
Contains a cache of XML Schema definition language (XSD) schemas.
Definition: XmlSchemaSet.cs:8
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16