// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System.IO;
-using System.Text;
-using System.Security;
-using System.Diagnostics;
using System.Collections;
+using System.Diagnostics;
using System.Globalization;
-using System.Xml.Schema;
+using System.IO;
using System.Runtime.Versioning;
+using System.Security;
+using System.Text;
+using System.Threading;
+using System.Xml.Schema;
namespace System.Xml
{
// Creates an XmlReader for parsing XML from the given Uri.
public static XmlReader Create(string inputUri)
{
- return XmlReader.Create(inputUri, (XmlReaderSettings)null, (XmlParserContext)null);
+ if (inputUri == null)
+ {
+ throw new ArgumentNullException(nameof(inputUri));
+ }
+ if (inputUri.Length == 0)
+ {
+ throw new ArgumentException(SR.XmlConvert_BadUri, nameof(inputUri));
+ }
+
+ // Avoid using XmlReader.Create(string, XmlReaderSettings), as it references a lot of types
+ // that then can't be trimmed away.
+ return new XmlTextReaderImpl(inputUri, XmlReaderSettings.s_defaultReaderSettings, null, new XmlUrlResolver());
}
// Creates an XmlReader according to the settings for parsing XML from the given Uri.
// Creates an XmlReader according to the settings and parser context for parsing XML from the given Uri.
public static XmlReader Create(string inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
{
- if (settings == null)
- {
- settings = new XmlReaderSettings();
- }
+ settings ??= XmlReaderSettings.s_defaultReaderSettings;
return settings.CreateReader(inputUri, inputContext);
}
// Creates an XmlReader according for parsing XML from the given stream.
public static XmlReader Create(Stream input)
{
- return Create(input, (XmlReaderSettings)null, (string)string.Empty);
+ if (input == null)
+ {
+ throw new ArgumentNullException(nameof(input));
+ }
+
+ // Avoid using XmlReader.Create(Stream, XmlReaderSettings), as it references a lot of types
+ // that then can't be trimmed away.
+ return new XmlTextReaderImpl(input, null, 0, XmlReaderSettings.s_defaultReaderSettings, null, string.Empty, null, false);
}
// Creates an XmlReader according to the settings for parsing XML from the given stream.
// Creates an XmlReader according to the settings and base Uri for parsing XML from the given stream.
public static XmlReader Create(Stream input, XmlReaderSettings settings, string baseUri)
{
- if (settings == null)
- {
- settings = new XmlReaderSettings();
- }
+ settings ??= XmlReaderSettings.s_defaultReaderSettings;
return settings.CreateReader(input, null, (string)baseUri, null);
}
// Creates an XmlReader according to the settings and parser context for parsing XML from the given stream.
public static XmlReader Create(Stream input, XmlReaderSettings settings, XmlParserContext inputContext)
{
- if (settings == null)
- {
- settings = new XmlReaderSettings();
- }
+ settings ??= XmlReaderSettings.s_defaultReaderSettings;
return settings.CreateReader(input, null, (string)string.Empty, inputContext);
}
// Creates an XmlReader according for parsing XML from the given TextReader.
public static XmlReader Create(TextReader input)
{
- return Create(input, (XmlReaderSettings)null, (string)string.Empty);
+ if (input == null)
+ {
+ throw new ArgumentNullException(nameof(input));
+ }
+
+ // Avoid using XmlReader.Create(TextReader, XmlReaderSettings), as it references a lot of types
+ // that then can't be trimmed away.
+ return new XmlTextReaderImpl(input, XmlReaderSettings.s_defaultReaderSettings, string.Empty, null);
}
// Creates an XmlReader according to the settings for parsing XML from the given TextReader.
// Creates an XmlReader according to the settings and baseUri for parsing XML from the given TextReader.
public static XmlReader Create(TextReader input, XmlReaderSettings settings, string baseUri)
{
- if (settings == null)
- {
- settings = new XmlReaderSettings();
- }
+ settings ??= XmlReaderSettings.s_defaultReaderSettings;
return settings.CreateReader(input, baseUri, null);
}
// Creates an XmlReader according to the settings and parser context for parsing XML from the given TextReader.
public static XmlReader Create(TextReader input, XmlReaderSettings settings, XmlParserContext inputContext)
{
- if (settings == null)
- {
- settings = new XmlReaderSettings();
- }
+ settings ??= XmlReaderSettings.s_defaultReaderSettings;
return settings.CreateReader(input, string.Empty, inputContext);
}
// Creates an XmlReader according to the settings wrapped over the given reader.
public static XmlReader Create(XmlReader reader, XmlReaderSettings settings)
{
- if (settings == null)
- {
- settings = new XmlReaderSettings();
- }
+ settings ??= XmlReaderSettings.s_defaultReaderSettings;
return settings.CreateReader(reader);
}
{
throw new ArgumentNullException(nameof(input));
}
- if (settings == null)
- {
- settings = new XmlReaderSettings();
- }
+ settings ??= XmlReaderSettings.s_defaultReaderSettings;
XmlReader reader;
// XmlReaderSettings class specifies basic features of an XmlReader.
public sealed class XmlReaderSettings
{
+ internal static readonly XmlReaderSettings s_defaultReaderSettings = new XmlReaderSettings() { ReadOnly = true };
+
//
// Fields
//
}
// resolve and open the url
- XmlResolver tmpResolver = this.GetXmlResolver();
- if (tmpResolver == null)
- {
- tmpResolver = CreateDefaultResolver();
- }
+ XmlResolver tmpResolver = this.GetXmlResolver() ?? new XmlUrlResolver();
// create text XML reader
XmlReader reader = new XmlTextReaderImpl(inputUri, this, inputContext, tmpResolver);
IsXmlResolverSet = false;
}
- private static XmlResolver CreateDefaultResolver()
- {
- return new XmlUrlResolver();
- }
-
internal XmlReader AddValidation(XmlReader reader)
{
XmlResolver resolver = null;
// Creates an XmlWriter for writing into the provided file.
public static XmlWriter Create(string outputFileName)
{
- return Create(outputFileName, null);
+ if (outputFileName == null)
+ {
+ throw new ArgumentNullException(nameof(outputFileName));
+ }
+
+ // Avoid using XmlWriter.Create(string, XmlReaderSettings), as it references a lot of types
+ // that then can't be trimmed away.
+ var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
+ try
+ {
+ var settings = new XmlWriterSettings() { CloseOutput = true };
+ XmlWriter writer = new XmlEncodedRawTextWriter(fs, settings);
+ return new XmlWellFormedWriter(writer, settings);
+ }
+ catch
+ {
+ fs.Dispose();
+ throw;
+ }
}
// Creates an XmlWriter for writing into the provided file with the specified settings.
public static XmlWriter Create(string outputFileName, XmlWriterSettings settings)
{
- if (settings == null)
- {
- settings = new XmlWriterSettings();
- }
+ settings ??= XmlWriterSettings.s_defaultWriterSettings;
return settings.CreateWriter(outputFileName);
}
// Creates an XmlWriter for writing into the provided stream.
public static XmlWriter Create(Stream output)
{
- return Create(output, null);
+ if (output == null)
+ {
+ throw new ArgumentNullException(nameof(output));
+ }
+
+ // Avoid using XmlWriter.Create(Stream, XmlReaderSettings), as it references a lot of types
+ // that then can't be trimmed away.
+ XmlWriterSettings settings = XmlWriterSettings.s_defaultWriterSettings;
+ XmlWriter writer = new XmlUtf8RawTextWriter(output, settings);
+ return new XmlWellFormedWriter(writer, settings);
}
// Creates an XmlWriter for writing into the provided stream with the specified settings.
public static XmlWriter Create(Stream output, XmlWriterSettings settings)
{
- if (settings == null)
- {
- settings = new XmlWriterSettings();
- }
+ settings ??= XmlWriterSettings.s_defaultWriterSettings;
return settings.CreateWriter(output);
}
// Creates an XmlWriter for writing into the provided TextWriter.
public static XmlWriter Create(TextWriter output)
{
- return Create(output, null);
+ if (output == null)
+ {
+ throw new ArgumentNullException(nameof(output));
+ }
+
+ // Avoid using XmlWriter.Create(TextWriter, XmlReaderSettings), as it references a lot of types
+ // that then can't be trimmed away.
+ XmlWriterSettings settings = XmlWriterSettings.s_defaultWriterSettings;
+ XmlWriter writer = new XmlEncodedRawTextWriter(output, settings);
+ return new XmlWellFormedWriter(writer, settings);
}
// Creates an XmlWriter for writing into the provided TextWriter with the specified settings.
public static XmlWriter Create(TextWriter output, XmlWriterSettings settings)
{
- if (settings == null)
- {
- settings = new XmlWriterSettings();
- }
+ settings ??= XmlWriterSettings.s_defaultWriterSettings;
return settings.CreateWriter(output);
}
// Creates an XmlWriter for writing into the provided StringBuilder.
public static XmlWriter Create(StringBuilder output)
{
- return Create(output, null);
+ if (output == null)
+ {
+ throw new ArgumentNullException(nameof(output));
+ }
+
+ // Avoid using XmlWriter.Create(StringBuilder, XmlReaderSettings), as it references a lot of types
+ // that then can't be trimmed away.
+ return Create(new StringWriter(output, CultureInfo.InvariantCulture));
}
// Creates an XmlWriter for writing into the provided StringBuilder with the specified settings.
public static XmlWriter Create(StringBuilder output, XmlWriterSettings settings)
{
- if (settings == null)
- {
- settings = new XmlWriterSettings();
- }
if (output == null)
{
throw new ArgumentNullException(nameof(output));
}
+
+ settings ??= XmlWriterSettings.s_defaultWriterSettings;
return settings.CreateWriter(new StringWriter(output, CultureInfo.InvariantCulture));
}
// Creates an XmlWriter wrapped around the provided XmlWriter with the specified settings.
public static XmlWriter Create(XmlWriter output, XmlWriterSettings settings)
{
- if (settings == null)
- {
- settings = new XmlWriterSettings();
- }
+ settings ??= XmlWriterSettings.s_defaultWriterSettings;
return settings.CreateWriter(output);
}
}
// XmlWriterSettings class specifies basic features of an XmlWriter.
public sealed class XmlWriterSettings
{
+ internal static readonly XmlWriterSettings s_defaultWriterSettings = new XmlWriterSettings() { ReadOnly = true };
+
//
// Fields
//
public sealed class XslCompiledTransform
{
- // Reader settings used when creating XmlReader from inputUri
- private static readonly XmlReaderSettings s_readerSettings = new XmlReaderSettings();
-
#if FEATURE_COMPILED_XSL
// Version for GeneratedCodeAttribute
private static readonly Version s_version = typeof(XslCompiledTransform).Assembly.GetName().Version;
public void Transform(string inputUri, XmlWriter results)
{
CheckArguments(inputUri, results);
- using (XmlReader reader = XmlReader.Create(inputUri, s_readerSettings))
+ using (XmlReader reader = XmlReader.Create(inputUri))
{
Transform(reader, (XsltArgumentList)null, results, CreateDefaultResolver());
}
public void Transform(string inputUri, XsltArgumentList arguments, XmlWriter results)
{
CheckArguments(inputUri, results);
- using (XmlReader reader = XmlReader.Create(inputUri, s_readerSettings))
+ using (XmlReader reader = XmlReader.Create(inputUri))
{
Transform(reader, arguments, results, CreateDefaultResolver());
}
public void Transform(string inputUri, XsltArgumentList arguments, TextWriter results)
{
CheckArguments(inputUri, results);
- using (XmlReader reader = XmlReader.Create(inputUri, s_readerSettings))
+ using (XmlReader reader = XmlReader.Create(inputUri))
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings))
{
Transform(reader, arguments, writer, CreateDefaultResolver());
public void Transform(string inputUri, XsltArgumentList arguments, Stream results)
{
CheckArguments(inputUri, results);
- using (XmlReader reader = XmlReader.Create(inputUri, s_readerSettings))
+ using (XmlReader reader = XmlReader.Create(inputUri))
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings))
{
Transform(reader, arguments, writer, CreateDefaultResolver());
throw new ArgumentNullException(nameof(resultsFile));
// SQLBUDT 276415: Prevent wiping out the content of the input file if the output file is the same
- using (XmlReader reader = XmlReader.Create(inputUri, s_readerSettings))
+ using (XmlReader reader = XmlReader.Create(inputUri))
using (XmlWriter writer = XmlWriter.Create(resultsFile, OutputSettings))
{
Transform(reader, (XsltArgumentList)null, writer, CreateDefaultResolver());