Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Xml / System / Xml / Core / XmlWriterSettings.cs / 2 / XmlWriterSettings.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml.Xsl.Runtime;
namespace System.Xml {
public enum XmlOutputMethod {
Xml = 0, // Use Xml 1.0 rules to serialize
Html = 1, // Use Html rules specified by Xslt specification to serialize
Text = 2, // Only serialize text blocks
AutoDetect = 3, // Choose between Xml and Html output methods at runtime (using Xslt rules to do so)
}
internal enum XmlStandalone {
// Do not change the constants - XmlBinaryWriter depends in it
Omit = 0,
Yes = 1,
No = 2,
}
///
/// Three-state logic enumeration.
///
internal enum TriState {
Unknown = -1,
False = 0,
True = 1,
};
// XmlReaderSettings class specifies features of an XmlWriter.
public sealed class XmlWriterSettings {
//
// Fields
//
// Text settings
Encoding encoding;
bool omitXmlDecl;
NewLineHandling newLineHandling;
string newLineChars;
TriState indent;
string indentChars;
bool newLineOnAttributes;
bool closeOutput;
// Conformance settings
ConformanceLevel conformanceLevel;
bool checkCharacters;
// Xslt settings
XmlOutputMethod outputMethod;
List cdataSections = new List();
bool mergeCDataSections;
string mediaType;
string docTypeSystem;
string docTypePublic;
XmlStandalone standalone;
bool autoXmlDecl;
// read-only flag
bool isReadOnly;
//
// Constructor
//
public XmlWriterSettings() {
Reset();
}
//
// Properties
//
// Text
public Encoding Encoding {
get {
return encoding;
}
set {
CheckReadOnly( "Encoding" );
encoding = value;
}
}
// True if an xml declaration should *not* be written.
public bool OmitXmlDeclaration {
get {
return omitXmlDecl;
}
set {
CheckReadOnly( "OmitXmlDeclaration" );
omitXmlDecl = value;
}
}
// See NewLineHandling enum for details.
public NewLineHandling NewLineHandling {
get {
return newLineHandling;
}
set {
CheckReadOnly("NewLineHandling");
if ( (uint)value > (uint)NewLineHandling.None ) {
throw new ArgumentOutOfRangeException( "value" );
}
newLineHandling = value;
}
}
// Line terminator string. By default, this is a carriage return followed by a line feed ("\r\n").
public string NewLineChars {
get {
return newLineChars;
}
set {
CheckReadOnly( "NewLineChars" );
if ( value == null ) {
throw new ArgumentNullException( "value" );
}
newLineChars = value;
}
}
// True if output should be indented using rules that are appropriate to the output rules (i.e. Xml, Html, etc).
public bool Indent {
get {
return indent == TriState.True;
}
set {
CheckReadOnly( "Indent" );
indent = value ? TriState.True : TriState.False;
}
}
// Characters to use when indenting. This is usually tab or some spaces, but can be anything.
public string IndentChars {
get {
return indentChars;
}
set {
CheckReadOnly( "IndentChars" );
if ( value == null ) {
throw new ArgumentNullException("value");
}
indentChars = value;
}
}
// Whether or not indent attributes on new lines.
public bool NewLineOnAttributes {
get {
return newLineOnAttributes;
}
set {
CheckReadOnly( "NewLineOnAttributes" );
newLineOnAttributes = value;
}
}
// Whether or not the XmlWriter should close the underlying stream or TextWriter when Close is called on the XmlWriter.
public bool CloseOutput {
get {
return closeOutput;
}
set {
CheckReadOnly( "CloseOutput" );
closeOutput = value;
}
}
// Conformance
// See ConformanceLevel enum for details.
public ConformanceLevel ConformanceLevel {
get {
return conformanceLevel;
}
set {
CheckReadOnly( "ConformanceLevel" );
if ( (uint)value > (uint)ConformanceLevel.Document ) {
throw new ArgumentOutOfRangeException( "value" );
}
conformanceLevel = value;
}
}
// Whether or not to check content characters that they are valid XML characters.
public bool CheckCharacters {
get {
return checkCharacters;
}
set {
CheckReadOnly( "CheckCharacters" );
checkCharacters = value;
}
}
//
// Public methods
//
public void Reset() {
encoding = Encoding.UTF8;
omitXmlDecl = false;
newLineHandling = NewLineHandling.Replace;
newLineChars = "\r\n";
indent = TriState.Unknown;
indentChars = " ";
newLineOnAttributes = false;
closeOutput = false;
conformanceLevel = ConformanceLevel.Document;
checkCharacters = true;
outputMethod = XmlOutputMethod.Xml;
cdataSections.Clear();
mergeCDataSections = false;
mediaType = null;
docTypeSystem = null;
docTypePublic = null;
standalone = XmlStandalone.Omit;
isReadOnly = false;
}
// Deep clone all settings (except read-only, which is always set to false). The original and new objects
// can now be set independently of each other.
public XmlWriterSettings Clone() {
XmlWriterSettings clonedSettings = MemberwiseClone() as XmlWriterSettings;
// Deep clone shared settings that are not immutable
clonedSettings.cdataSections = new List( cdataSections );
// Read-only setting is always false on clones
clonedSettings.isReadOnly = false;
return clonedSettings;
}
//
// Internal and private methods
//
internal bool ReadOnly {
get {
return isReadOnly;
}
set {
isReadOnly = value;
}
}
// Specifies the method (Html, Xml, etc.) that will be used to serialize the result tree.
public XmlOutputMethod OutputMethod {
get {
return outputMethod;
}
internal set {
outputMethod = value;
}
}
// Set of XmlQualifiedNames that identify any elements that need to have text children wrapped in CData sections.
internal List CDataSectionElements {
get {
Debug.Assert(cdataSections != null);
return cdataSections;
}
}
internal bool MergeCDataSections {
get {
return mergeCDataSections;
}
set {
CheckReadOnly( "MergeCDataSections" );
mergeCDataSections = value;
}
}
// Used in Html writer when writing Meta element. Null denotes the default media type.
internal string MediaType {
get {
return mediaType;
}
set {
CheckReadOnly( "MediaType" );
mediaType = value;
}
}
// System Id in doc-type declaration. Null denotes the absence of the system Id.
internal string DocTypeSystem {
get {
return docTypeSystem;
}
set {
CheckReadOnly( "DocTypeSystem" );
docTypeSystem = value;
}
}
// Public Id in doc-type declaration. Null denotes the absence of the public Id.
internal string DocTypePublic {
get {
return docTypePublic;
}
set {
CheckReadOnly( "DocTypePublic" );
docTypePublic = value;
}
}
// Yes for standalone="yes", No for standalone="no", and Omit for no standalone.
internal XmlStandalone Standalone {
get {
return standalone;
}
set {
CheckReadOnly( "Standalone" );
standalone = value;
}
}
// True if an xml declaration should automatically be output (no need to call WriteStartDocument)
internal bool AutoXmlDeclaration {
get {
return autoXmlDecl;
}
set {
CheckReadOnly( "AutoXmlDeclaration" );
autoXmlDecl = value;
}
}
// If TriState.Unknown, then Indent property was not explicitly set. In this case, the AutoDetect output
// method will default to Indent=true for Html and Indent=false for Xml.
internal TriState InternalIndent {
get {
return indent;
}
}
internal bool IsQuerySpecific {
get {
return cdataSections.Count != 0 || docTypePublic != null ||
docTypeSystem != null || standalone == XmlStandalone.Yes;
}
}
private void CheckReadOnly( string propertyName ) {
if ( isReadOnly ) {
throw new XmlException( Res.Xml_ReadOnlyProperty, "XmlWriterSettings." + propertyName );
}
}
#if !HIDE_XSL
///
/// Serialize the object to BinaryWriter.
///
internal void GetObjectData(XmlQueryDataWriter writer) {
// Encoding encoding;
// NOTE: For Encoding we serialize only CodePage, and ignore EncoderFallback/DecoderFallback.
// It suffices for XSLT purposes, but not in the general case.
Debug.Assert(encoding.Equals(Encoding.GetEncoding(encoding.CodePage)), "Cannot serialize encoding correctly");
writer.Write(encoding.CodePage);
// bool omitXmlDecl;
writer.Write(omitXmlDecl);
// NewLineHandling newLineHandling;
writer.Write((sbyte)newLineHandling);
// string newLineChars;
writer.WriteStringQ(newLineChars);
// TriState indent;
writer.Write((sbyte)indent);
// string indentChars;
writer.WriteStringQ(indentChars);
// bool newLineOnAttributes;
writer.Write(newLineOnAttributes);
// bool closeOutput;
writer.Write(closeOutput);
// ConformanceLevel conformanceLevel;
writer.Write((sbyte)conformanceLevel);
// bool checkCharacters;
writer.Write(checkCharacters);
// XmlOutputMethod outputMethod;
writer.Write((sbyte)outputMethod);
// List cdataSections;
writer.Write(cdataSections.Count);
foreach (XmlQualifiedName qname in cdataSections) {
writer.Write(qname.Name);
writer.Write(qname.Namespace);
}
// bool mergeCDataSections;
writer.Write(mergeCDataSections);
// string mediaType;
writer.WriteStringQ(mediaType);
// string docTypeSystem;
writer.WriteStringQ(docTypeSystem);
// string docTypePublic;
writer.WriteStringQ(docTypePublic);
// XmlStandalone standalone;
writer.Write((sbyte)standalone);
// bool autoXmlDecl;
writer.Write(autoXmlDecl);
// bool isReadOnly;
writer.Write(isReadOnly);
}
///
/// Deserialize the object from BinaryReader.
///
internal XmlWriterSettings(XmlQueryDataReader reader) {
// Encoding encoding;
encoding = Encoding.GetEncoding(reader.ReadInt32());
// bool omitXmlDecl;
omitXmlDecl = reader.ReadBoolean();
// NewLineHandling newLineHandling;
newLineHandling = (NewLineHandling)reader.ReadSByte(0, (sbyte)NewLineHandling.None);
// string newLineChars;
newLineChars = reader.ReadStringQ();
// TriState indent;
indent = (TriState)reader.ReadSByte((sbyte)TriState.Unknown, (sbyte)TriState.True);
// string indentChars;
indentChars = reader.ReadStringQ();
// bool newLineOnAttributes;
newLineOnAttributes = reader.ReadBoolean();
// bool closeOutput;
closeOutput = reader.ReadBoolean();
// ConformanceLevel conformanceLevel;
conformanceLevel = (ConformanceLevel)reader.ReadSByte(0, (sbyte)ConformanceLevel.Document);
// bool checkCharacters;
checkCharacters = reader.ReadBoolean();
// XmlOutputMethod outputMethod;
outputMethod = (XmlOutputMethod)reader.ReadSByte(0, (sbyte)XmlOutputMethod.AutoDetect);
// List cdataSections;
int length = reader.ReadInt32();
cdataSections = new List(length);
for (int idx = 0; idx < length; idx++) {
cdataSections.Add(new XmlQualifiedName(reader.ReadString(), reader.ReadString()));
}
// bool mergeCDataSections;
mergeCDataSections = reader.ReadBoolean();
// string mediaType;
mediaType = reader.ReadStringQ();
// string docTypeSystem;
docTypeSystem = reader.ReadStringQ();
// string docTypePublic;
docTypePublic = reader.ReadStringQ();
// XmlStandalone standalone;
Standalone = (XmlStandalone)reader.ReadSByte(0, (sbyte)XmlStandalone.No);
// bool autoXmlDecl;
autoXmlDecl = reader.ReadBoolean();
// bool isReadOnly;
isReadOnly = reader.ReadBoolean();
}
#else
internal void GetObjectData(object writer) { }
internal XmlWriterSettings(object reader) { }
#endif
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataGridClipboardCellContent.cs
- TraceHandler.cs
- PageAsyncTask.cs
- AppearanceEditorPart.cs
- TdsParser.cs
- TagNameToTypeMapper.cs
- EllipseGeometry.cs
- HtmlDocument.cs
- EntityCollection.cs
- ToolStripItemCollection.cs
- TextDecorationUnitValidation.cs
- login.cs
- SafeNativeMemoryHandle.cs
- LinkLabel.cs
- StylusButtonEventArgs.cs
- DataGridBeginningEditEventArgs.cs
- StateMachineDesignerPaint.cs
- PersonalizationProvider.cs
- CombinedTcpChannel.cs
- TemplateControl.cs
- ValueUtilsSmi.cs
- WebServiceTypeData.cs
- DescendentsWalkerBase.cs
- DataGridItem.cs
- EmbeddedMailObjectsCollection.cs
- Point3DCollection.cs
- SecurityElement.cs
- SafeProcessHandle.cs
- FixedSOMPageConstructor.cs
- PropertyDescriptorComparer.cs
- VisualTreeUtils.cs
- BindingExpression.cs
- DiffuseMaterial.cs
- ToolStripDropDown.cs
- KeyboardNavigation.cs
- GridViewDeletedEventArgs.cs
- Base64Decoder.cs
- ZipIOCentralDirectoryFileHeader.cs
- UMPAttributes.cs
- Visitor.cs
- GridViewAutomationPeer.cs
- Activator.cs
- MemberDescriptor.cs
- FlowDocumentScrollViewerAutomationPeer.cs
- WebPartMovingEventArgs.cs
- ConnectionInterfaceCollection.cs
- FileEnumerator.cs
- HttpCacheVaryByContentEncodings.cs
- TraceHandlerErrorFormatter.cs
- ColorTransform.cs
- WebPartHeaderCloseVerb.cs
- XmlLinkedNode.cs
- String.cs
- MatcherBuilder.cs
- SqlUserDefinedTypeAttribute.cs
- Helpers.cs
- AttributeSetAction.cs
- XmlDataLoader.cs
- CollectionEditor.cs
- DataTableMapping.cs
- InvokeWebService.cs
- WindowsGraphics2.cs
- IMembershipProvider.cs
- SiteMapNodeCollection.cs
- DrawingBrush.cs
- SecondaryIndexDefinition.cs
- ReversePositionQuery.cs
- DynamicRendererThreadManager.cs
- X509CertificateCollection.cs
- DnsPermission.cs
- EntityConnection.cs
- PortCache.cs
- DirectoryInfo.cs
- InsufficientMemoryException.cs
- DefaultShape.cs
- FontWeights.cs
- AddInSegmentDirectoryNotFoundException.cs
- CustomTokenProvider.cs
- ProjectionQueryOptionExpression.cs
- CaseExpr.cs
- ColorContextHelper.cs
- Assert.cs
- XmlDeclaration.cs
- AllowedAudienceUriElementCollection.cs
- FrameworkPropertyMetadata.cs
- ValidatorCompatibilityHelper.cs
- XmlSchemas.cs
- LoginCancelEventArgs.cs
- CreatingCookieEventArgs.cs
- ApplicationManager.cs
- CopyAttributesAction.cs
- MergePropertyDescriptor.cs
- WebPartConnectionsDisconnectVerb.cs
- TextEditorParagraphs.cs
- MetadataProperty.cs
- TypeDescriptor.cs
- TextInfo.cs
- FileAuthorizationModule.cs
- StrokeCollection.cs
- HtmlInputRadioButton.cs