mscorlib(4.0.0.0) API with additions
XmlWriterSettings.cs
2 using System.IO;
4 using System.Text;
5 using System.Xml.Xsl.Runtime;
6 
7 namespace System.Xml
8 {
10  [global::__DynamicallyInvokable]
11  [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
12  public sealed class XmlWriterSettings
13  {
14  private bool useAsync;
15 
16  private Encoding encoding;
17 
18  private bool omitXmlDecl;
19 
20  private NewLineHandling newLineHandling;
21 
22  private string newLineChars;
23 
24  private TriState indent;
25 
26  private string indentChars;
27 
28  private bool newLineOnAttributes;
29 
30  private bool closeOutput;
31 
32  private NamespaceHandling namespaceHandling;
33 
34  private ConformanceLevel conformanceLevel;
35 
36  private bool checkCharacters;
37 
38  private bool writeEndDocumentOnClose;
39 
40  private XmlOutputMethod outputMethod;
41 
42  private List<XmlQualifiedName> cdataSections = new List<XmlQualifiedName>();
43 
44  private bool doNotEscapeUriAttributes;
45 
46  private bool mergeCDataSections;
47 
48  private string mediaType;
49 
50  private string docTypeSystem;
51 
52  private string docTypePublic;
53 
54  private XmlStandalone standalone;
55 
56  private bool autoXmlDecl;
57 
58  private bool isReadOnly;
59 
63  [global::__DynamicallyInvokable]
64  public bool Async
65  {
66  [global::__DynamicallyInvokable]
67  get
68  {
69  return useAsync;
70  }
71  [global::__DynamicallyInvokable]
72  set
73  {
74  CheckReadOnly("Async");
75  useAsync = value;
76  }
77  }
78 
81  [global::__DynamicallyInvokable]
82  public Encoding Encoding
83  {
84  [global::__DynamicallyInvokable]
85  get
86  {
87  return encoding;
88  }
89  [global::__DynamicallyInvokable]
90  set
91  {
92  CheckReadOnly("Encoding");
93  encoding = value;
94  }
95  }
96 
100  [global::__DynamicallyInvokable]
101  public bool OmitXmlDeclaration
102  {
103  [global::__DynamicallyInvokable]
104  get
105  {
106  return omitXmlDecl;
107  }
108  [global::__DynamicallyInvokable]
109  set
110  {
111  CheckReadOnly("OmitXmlDeclaration");
112  omitXmlDecl = value;
113  }
114  }
115 
118  [global::__DynamicallyInvokable]
120  {
121  [global::__DynamicallyInvokable]
122  get
123  {
124  return newLineHandling;
125  }
126  [global::__DynamicallyInvokable]
127  set
128  {
129  CheckReadOnly("NewLineHandling");
130  if ((uint)value > 2u)
131  {
132  throw new ArgumentOutOfRangeException("value");
133  }
134  newLineHandling = value;
135  }
136  }
137 
141  [global::__DynamicallyInvokable]
142  public string NewLineChars
143  {
144  [global::__DynamicallyInvokable]
145  get
146  {
147  return newLineChars;
148  }
149  [global::__DynamicallyInvokable]
150  set
151  {
152  CheckReadOnly("NewLineChars");
153  if (value == null)
154  {
155  throw new ArgumentNullException("value");
156  }
157  newLineChars = value;
158  }
159  }
160 
164  [global::__DynamicallyInvokable]
165  public bool Indent
166  {
167  [global::__DynamicallyInvokable]
168  get
169  {
170  return indent == TriState.True;
171  }
172  [global::__DynamicallyInvokable]
173  set
174  {
175  CheckReadOnly("Indent");
176  indent = (value ? TriState.True : TriState.False);
177  }
178  }
179 
183  [global::__DynamicallyInvokable]
184  public string IndentChars
185  {
186  [global::__DynamicallyInvokable]
187  get
188  {
189  return indentChars;
190  }
191  [global::__DynamicallyInvokable]
192  set
193  {
194  CheckReadOnly("IndentChars");
195  if (value == null)
196  {
197  throw new ArgumentNullException("value");
198  }
199  indentChars = value;
200  }
201  }
202 
206  [global::__DynamicallyInvokable]
207  public bool NewLineOnAttributes
208  {
209  [global::__DynamicallyInvokable]
210  get
211  {
212  return newLineOnAttributes;
213  }
214  [global::__DynamicallyInvokable]
215  set
216  {
217  CheckReadOnly("NewLineOnAttributes");
218  newLineOnAttributes = value;
219  }
220  }
221 
225  [global::__DynamicallyInvokable]
226  public bool CloseOutput
227  {
228  [global::__DynamicallyInvokable]
229  get
230  {
231  return closeOutput;
232  }
233  [global::__DynamicallyInvokable]
234  set
235  {
236  CheckReadOnly("CloseOutput");
237  closeOutput = value;
238  }
239  }
240 
243  [global::__DynamicallyInvokable]
245  {
246  [global::__DynamicallyInvokable]
247  get
248  {
249  return conformanceLevel;
250  }
251  [global::__DynamicallyInvokable]
252  set
253  {
254  CheckReadOnly("ConformanceLevel");
255  if ((uint)value > 2u)
256  {
257  throw new ArgumentOutOfRangeException("value");
258  }
259  conformanceLevel = value;
260  }
261  }
262 
266  [global::__DynamicallyInvokable]
267  public bool CheckCharacters
268  {
269  [global::__DynamicallyInvokable]
270  get
271  {
272  return checkCharacters;
273  }
274  [global::__DynamicallyInvokable]
275  set
276  {
277  CheckReadOnly("CheckCharacters");
278  checkCharacters = value;
279  }
280  }
281 
284  [global::__DynamicallyInvokable]
286  {
287  [global::__DynamicallyInvokable]
288  get
289  {
290  return namespaceHandling;
291  }
292  [global::__DynamicallyInvokable]
293  set
294  {
295  CheckReadOnly("NamespaceHandling");
296  if ((uint)value > 1u)
297  {
298  throw new ArgumentOutOfRangeException("value");
299  }
300  namespaceHandling = value;
301  }
302  }
303 
307  [global::__DynamicallyInvokable]
308  public bool WriteEndDocumentOnClose
309  {
310  [global::__DynamicallyInvokable]
311  get
312  {
313  return writeEndDocumentOnClose;
314  }
315  [global::__DynamicallyInvokable]
316  set
317  {
318  CheckReadOnly("WriteEndDocumentOnClose");
319  writeEndDocumentOnClose = value;
320  }
321  }
322 
326  {
327  get
328  {
329  return outputMethod;
330  }
331  internal set
332  {
333  outputMethod = value;
334  }
335  }
336 
337  internal List<XmlQualifiedName> CDataSectionElements => cdataSections;
338 
342  public bool DoNotEscapeUriAttributes
343  {
344  get
345  {
346  return doNotEscapeUriAttributes;
347  }
348  set
349  {
350  CheckReadOnly("DoNotEscapeUriAttributes");
351  doNotEscapeUriAttributes = value;
352  }
353  }
354 
355  internal bool MergeCDataSections
356  {
357  get
358  {
359  return mergeCDataSections;
360  }
361  set
362  {
363  CheckReadOnly("MergeCDataSections");
364  mergeCDataSections = value;
365  }
366  }
367 
368  internal string MediaType
369  {
370  get
371  {
372  return mediaType;
373  }
374  set
375  {
376  CheckReadOnly("MediaType");
377  mediaType = value;
378  }
379  }
380 
381  internal string DocTypeSystem
382  {
383  get
384  {
385  return docTypeSystem;
386  }
387  set
388  {
389  CheckReadOnly("DocTypeSystem");
390  docTypeSystem = value;
391  }
392  }
393 
394  internal string DocTypePublic
395  {
396  get
397  {
398  return docTypePublic;
399  }
400  set
401  {
402  CheckReadOnly("DocTypePublic");
403  docTypePublic = value;
404  }
405  }
406 
407  internal XmlStandalone Standalone
408  {
409  get
410  {
411  return standalone;
412  }
413  set
414  {
415  CheckReadOnly("Standalone");
416  standalone = value;
417  }
418  }
419 
420  internal bool AutoXmlDeclaration
421  {
422  get
423  {
424  return autoXmlDecl;
425  }
426  set
427  {
428  CheckReadOnly("AutoXmlDeclaration");
429  autoXmlDecl = value;
430  }
431  }
432 
433  internal TriState IndentInternal
434  {
435  get
436  {
437  return indent;
438  }
439  set
440  {
441  indent = value;
442  }
443  }
444 
445  internal bool IsQuerySpecific
446  {
447  get
448  {
449  if (cdataSections.Count == 0 && docTypePublic == null && docTypeSystem == null)
450  {
451  return standalone == XmlStandalone.Yes;
452  }
453  return true;
454  }
455  }
456 
457  internal bool ReadOnly
458  {
459  get
460  {
461  return isReadOnly;
462  }
463  set
464  {
465  isReadOnly = value;
466  }
467  }
468 
470  [global::__DynamicallyInvokable]
472  {
473  Initialize();
474  }
475 
477  [global::__DynamicallyInvokable]
478  public void Reset()
479  {
480  CheckReadOnly("Reset");
481  Initialize();
482  }
483 
486  [global::__DynamicallyInvokable]
488  {
489  XmlWriterSettings xmlWriterSettings = MemberwiseClone() as XmlWriterSettings;
490  xmlWriterSettings.cdataSections = new List<XmlQualifiedName>(cdataSections);
491  xmlWriterSettings.isReadOnly = false;
492  return xmlWriterSettings;
493  }
494 
495  internal XmlWriter CreateWriter(string outputFileName)
496  {
497  if (outputFileName == null)
498  {
499  throw new ArgumentNullException("outputFileName");
500  }
501  XmlWriterSettings xmlWriterSettings = this;
502  if (!xmlWriterSettings.CloseOutput)
503  {
504  xmlWriterSettings = xmlWriterSettings.Clone();
505  xmlWriterSettings.CloseOutput = true;
506  }
507  FileStream fileStream = null;
508  try
509  {
510  fileStream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, useAsync);
511  return xmlWriterSettings.CreateWriter(fileStream);
512  }
513  catch
514  {
515  fileStream?.Close();
516  throw;
517  }
518  }
519 
520  internal XmlWriter CreateWriter(Stream output)
521  {
522  if (output == null)
523  {
524  throw new ArgumentNullException("output");
525  }
526  XmlWriter xmlWriter;
527  if (Encoding.WebName == "utf-8")
528  {
529  switch (OutputMethod)
530  {
531  case XmlOutputMethod.Xml:
532  xmlWriter = ((!Indent) ? new XmlUtf8RawTextWriter(output, this) : new XmlUtf8RawTextWriterIndent(output, this));
533  break;
534  case XmlOutputMethod.Html:
535  xmlWriter = ((!Indent) ? new HtmlUtf8RawTextWriter(output, this) : new HtmlUtf8RawTextWriterIndent(output, this));
536  break;
537  case XmlOutputMethod.Text:
538  xmlWriter = new TextUtf8RawTextWriter(output, this);
539  break;
540  case XmlOutputMethod.AutoDetect:
541  xmlWriter = new XmlAutoDetectWriter(output, this);
542  break;
543  default:
544  return null;
545  }
546  }
547  else
548  {
549  switch (OutputMethod)
550  {
551  case XmlOutputMethod.Xml:
552  xmlWriter = ((!Indent) ? new XmlEncodedRawTextWriter(output, this) : new XmlEncodedRawTextWriterIndent(output, this));
553  break;
554  case XmlOutputMethod.Html:
555  xmlWriter = ((!Indent) ? new HtmlEncodedRawTextWriter(output, this) : new HtmlEncodedRawTextWriterIndent(output, this));
556  break;
557  case XmlOutputMethod.Text:
558  xmlWriter = new TextEncodedRawTextWriter(output, this);
559  break;
560  case XmlOutputMethod.AutoDetect:
561  xmlWriter = new XmlAutoDetectWriter(output, this);
562  break;
563  default:
564  return null;
565  }
566  }
567  if (OutputMethod != XmlOutputMethod.AutoDetect && IsQuerySpecific)
568  {
569  xmlWriter = new QueryOutputWriter((XmlRawWriter)xmlWriter, this);
570  }
571  xmlWriter = new XmlWellFormedWriter(xmlWriter, this);
572  if (useAsync)
573  {
574  xmlWriter = new XmlAsyncCheckWriter(xmlWriter);
575  }
576  return xmlWriter;
577  }
578 
579  internal XmlWriter CreateWriter(TextWriter output)
580  {
581  if (output == null)
582  {
583  throw new ArgumentNullException("output");
584  }
585  XmlWriter xmlWriter;
586  switch (OutputMethod)
587  {
588  case XmlOutputMethod.Xml:
589  xmlWriter = ((!Indent) ? new XmlEncodedRawTextWriter(output, this) : new XmlEncodedRawTextWriterIndent(output, this));
590  break;
591  case XmlOutputMethod.Html:
592  xmlWriter = ((!Indent) ? new HtmlEncodedRawTextWriter(output, this) : new HtmlEncodedRawTextWriterIndent(output, this));
593  break;
594  case XmlOutputMethod.Text:
595  xmlWriter = new TextEncodedRawTextWriter(output, this);
596  break;
597  case XmlOutputMethod.AutoDetect:
598  xmlWriter = new XmlAutoDetectWriter(output, this);
599  break;
600  default:
601  return null;
602  }
603  if (OutputMethod != XmlOutputMethod.AutoDetect && IsQuerySpecific)
604  {
605  xmlWriter = new QueryOutputWriter((XmlRawWriter)xmlWriter, this);
606  }
607  xmlWriter = new XmlWellFormedWriter(xmlWriter, this);
608  if (useAsync)
609  {
610  xmlWriter = new XmlAsyncCheckWriter(xmlWriter);
611  }
612  return xmlWriter;
613  }
614 
615  internal XmlWriter CreateWriter(XmlWriter output)
616  {
617  if (output == null)
618  {
619  throw new ArgumentNullException("output");
620  }
621  return AddConformanceWrapper(output);
622  }
623 
624  private void CheckReadOnly(string propertyName)
625  {
626  if (isReadOnly)
627  {
628  throw new XmlException("Xml_ReadOnlyProperty", GetType().Name + "." + propertyName);
629  }
630  }
631 
632  private void Initialize()
633  {
634  encoding = Encoding.UTF8;
635  omitXmlDecl = false;
636  newLineHandling = NewLineHandling.Replace;
637  newLineChars = Environment.NewLine;
638  indent = TriState.Unknown;
639  indentChars = " ";
640  newLineOnAttributes = false;
641  closeOutput = false;
642  namespaceHandling = NamespaceHandling.Default;
643  conformanceLevel = ConformanceLevel.Document;
644  checkCharacters = true;
645  writeEndDocumentOnClose = true;
646  outputMethod = XmlOutputMethod.Xml;
647  cdataSections.Clear();
648  mergeCDataSections = false;
649  mediaType = null;
650  docTypeSystem = null;
651  docTypePublic = null;
652  standalone = XmlStandalone.Omit;
653  doNotEscapeUriAttributes = false;
654  useAsync = false;
655  isReadOnly = false;
656  }
657 
658  private XmlWriter AddConformanceWrapper(XmlWriter baseWriter)
659  {
660  ConformanceLevel conformanceLevel = ConformanceLevel.Auto;
661  XmlWriterSettings settings = baseWriter.Settings;
662  bool flag = false;
663  bool checkNames = false;
664  bool flag2 = false;
665  bool flag3 = false;
666  if (settings == null)
667  {
668  if (newLineHandling == NewLineHandling.Replace)
669  {
670  flag2 = true;
671  flag3 = true;
672  }
673  if (checkCharacters)
674  {
675  flag = true;
676  flag3 = true;
677  }
678  }
679  else
680  {
681  if (this.conformanceLevel != settings.ConformanceLevel)
682  {
683  conformanceLevel = ConformanceLevel;
684  flag3 = true;
685  }
686  if (checkCharacters && !settings.CheckCharacters)
687  {
688  flag = true;
689  checkNames = (conformanceLevel == ConformanceLevel.Auto);
690  flag3 = true;
691  }
692  if (newLineHandling == NewLineHandling.Replace && settings.NewLineHandling == NewLineHandling.None)
693  {
694  flag2 = true;
695  flag3 = true;
696  }
697  }
698  XmlWriter xmlWriter = baseWriter;
699  if (flag3)
700  {
701  if (conformanceLevel != 0)
702  {
703  xmlWriter = new XmlWellFormedWriter(xmlWriter, this);
704  }
705  if (flag | flag2)
706  {
707  xmlWriter = new XmlCharCheckingWriter(xmlWriter, flag, checkNames, flag2, NewLineChars);
708  }
709  }
710  if (IsQuerySpecific && (settings == null || !settings.IsQuerySpecific))
711  {
712  xmlWriter = new QueryOutputWriterV1(xmlWriter, this);
713  }
714  return xmlWriter;
715  }
716 
717  internal void GetObjectData(XmlQueryDataWriter writer)
718  {
719  writer.Write(Encoding.CodePage);
720  writer.Write(OmitXmlDeclaration);
721  writer.Write((sbyte)NewLineHandling);
722  writer.WriteStringQ(NewLineChars);
723  writer.Write((sbyte)IndentInternal);
724  writer.WriteStringQ(IndentChars);
725  writer.Write(NewLineOnAttributes);
726  writer.Write(CloseOutput);
727  writer.Write((sbyte)ConformanceLevel);
728  writer.Write(CheckCharacters);
729  writer.Write((sbyte)outputMethod);
730  writer.Write(cdataSections.Count);
731  foreach (XmlQualifiedName cdataSection in cdataSections)
732  {
733  writer.Write(cdataSection.Name);
734  writer.Write(cdataSection.Namespace);
735  }
736  writer.Write(mergeCDataSections);
737  writer.WriteStringQ(mediaType);
738  writer.WriteStringQ(docTypeSystem);
739  writer.WriteStringQ(docTypePublic);
740  writer.Write((sbyte)standalone);
741  writer.Write(autoXmlDecl);
742  writer.Write(ReadOnly);
743  }
744 
745  internal XmlWriterSettings(XmlQueryDataReader reader)
746  {
747  Encoding = Encoding.GetEncoding(reader.ReadInt32());
748  OmitXmlDeclaration = reader.ReadBoolean();
749  NewLineHandling = (NewLineHandling)reader.ReadSByte(0, 2);
750  NewLineChars = reader.ReadStringQ();
751  IndentInternal = (TriState)reader.ReadSByte(-1, 1);
752  IndentChars = reader.ReadStringQ();
753  NewLineOnAttributes = reader.ReadBoolean();
754  CloseOutput = reader.ReadBoolean();
755  ConformanceLevel = (ConformanceLevel)reader.ReadSByte(0, 2);
756  CheckCharacters = reader.ReadBoolean();
757  outputMethod = (XmlOutputMethod)reader.ReadSByte(0, 3);
758  int num = reader.ReadInt32();
759  cdataSections = new List<XmlQualifiedName>(num);
760  for (int i = 0; i < num; i++)
761  {
762  cdataSections.Add(new XmlQualifiedName(reader.ReadString(), reader.ReadString()));
763  }
764  mergeCDataSections = reader.ReadBoolean();
765  mediaType = reader.ReadStringQ();
766  docTypeSystem = reader.ReadStringQ();
767  docTypePublic = reader.ReadStringQ();
768  Standalone = (XmlStandalone)reader.ReadSByte(0, 2);
769  autoXmlDecl = reader.ReadBoolean();
770  ReadOnly = reader.ReadBoolean();
771  }
772  }
773 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
bool CheckCharacters
Gets or sets a value that indicates whether the XML writer should check to ensure that all characters...
XmlWriterSettings Clone()
Creates a copy of the T:System.Xml.XmlWriterSettings instance.
bool OmitXmlDeclaration
Gets or sets a value indicating whether to omit an XML declaration.
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
string IndentChars
Gets or sets the character string to use when indenting. This setting is used when the P:System....
bool? Indent
Gets or sets a value indicating whether to indent elements.
FileMode
Specifies how the operating system should open a file.
Definition: FileMode.cs:8
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
NamespaceHandling NamespaceHandling
Gets or sets a value that indicates whether the T:System.Xml.XmlWriter should remove duplicate namesp...
bool NewLineOnAttributes
Gets or sets a value indicating whether to write attributes on a new line.
static Encoding GetEncoding(int codepage)
Returns the encoding associated with the specified code page identifier.
Definition: Encoding.cs:1249
NewLineHandling
Specifies how to handle line breaks.
Represents a writer that provides a fast, non-cached, forward-only way to generate streams or files t...
Definition: XmlWriter.cs:12
string NewLineChars
Gets or sets the character string to use for line breaks.
SecurityAction
Specifies the security actions that can be performed using declarative security.
virtual void Close()
Closes the current stream and releases any resources (such as sockets and file handles) associated wi...
Definition: Stream.cs:855
void Reset()
Resets the members of the settings class to their default values.
ConformanceLevel ConformanceLevel
Gets or sets the level of conformance that the XML writer checks the XML output for.
virtual string WebName
When overridden in a derived class, gets the name registered with the Internet Assigned Numbers Autho...
Definition: Encoding.cs:750
virtual int CodePage
When overridden in a derived class, gets the code page identifier of the current T:System....
Definition: Encoding.cs:948
ConformanceLevel
Specifies the amount of input or output checking that T:System.Xml.XmlReader and T:System....
NamespaceHandling
Specifies whether to remove duplicate namespace declarations in the T:System.Xml.XmlWriter.
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
Definition: FileStream.cs:15
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
bool WriteEndDocumentOnClose
Gets or sets a value that indicates whether the T:System.Xml.XmlWriter will add closing tags to all u...
XmlOutputMethod OutputMethod
Gets the method used to serialize the T:System.Xml.XmlWriter output.
NewLineHandling NewLineHandling
Gets or sets a value indicating whether to normalize line breaks in the output.
Specifies a set of features to support on the T:System.Xml.XmlWriter object created by the Overload:S...
void Clear()
Removes all elements from the T:System.Collections.Generic.List`1.
Definition: List.cs:614
bool Async
Gets or sets a value that indicates whether asynchronous T:System.Xml.XmlWriter methods can be used o...
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
static Encoding UTF8
Gets an encoding for the UTF-8 format.
Definition: Encoding.cs:1023
bool DoNotEscapeUriAttributes
Gets or sets a value that indicates whether the T:System.Xml.XmlWriter does not escape URI attributes...
XmlOutputMethod
Specifies the method used to serialize the T:System.Xml.XmlWriter output.
XmlWriterSettings()
Initializes a new instance of the T:System.Xml.XmlWriterSettings class.
FileShare
Contains constants for controlling the kind of access other T:System.IO.FileStream objects can have t...
Definition: FileShare.cs:9
bool CloseOutput
Gets or sets a value indicating whether the T:System.Xml.XmlWriter should also close the underlying s...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16