Skip to content

Commit

Permalink
Initialize XmlReaders with a stream
Browse files Browse the repository at this point in the history
This prevents multi-byte characters from
getting mangled when converting a filepath to a URI
  • Loading branch information
benvillalobos committed Sep 14, 2021
1 parent ea1d6d9 commit 6943a55
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
12 changes: 4 additions & 8 deletions src/Build/Xml/XmlReaderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ private XmlReaderExtension(string file, bool loadAsReadOnly)
_streamReader = new StreamReader(_stream, s_utf8NoBom, detectEncodingFromByteOrderMarks: true);
Encoding detectedEncoding;

// The XmlDocumentWithWithLocation class relies on the reader's BaseURI property to be set,
// thus we pass the document's file path to the appropriate xml reader constructor.
Reader = GetXmlReader(file, _streamReader, loadAsReadOnly, out detectedEncoding);
Reader = GetXmlReader(_streamReader, out detectedEncoding);

// Override detected encoding if an XML encoding attribute is specified and that encoding is sufficiently
// different from the detected encoding.
Expand Down Expand Up @@ -71,15 +69,13 @@ public void Dispose()
_stream?.Dispose();
}

private static XmlReader GetXmlReader(string file, StreamReader input, bool loadAsReadOnly, out Encoding encoding)
private static XmlReader GetXmlReader(StreamReader input, out Encoding encoding)
{
string uri = new UriBuilder(Uri.UriSchemeFile, string.Empty) { Path = file }.ToString();


// Ignore loadAsReadOnly for now; using XmlReader.Create results in whitespace changes
// of attribute text, specifically newline removal.
// https://github.com/Microsoft/msbuild/issues/4210
XmlReader reader = new XmlTextReader(uri, input) { DtdProcessing = DtdProcessing.Ignore };
XmlReader reader = XmlTextReader.Create(input);
reader.Settings.DtdProcessing = DtdProcessing.Ignore;

reader.Read();
encoding = input.CurrentEncoding;
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/XslTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public XmlReader CreateReader(int itemPos)
{
if (XmlMode == XmlModes.XmlFile)
{
return XmlReader.Create(_data[itemPos]);
return XmlReader.Create(new StreamReader(_data[itemPos]));
}
else // xmlModes.Xml
{
Expand Down Expand Up @@ -459,7 +459,7 @@ public XslCompiledTransform LoadXslt(bool useTrustedSettings)
_log.LogMessageFromResources(MessageImportance.Low, "XslTransform.UseTrustedSettings", _data);
}

xslct.Load(new XPathDocument(XmlReader.Create(_data)), settings, new XmlUrlResolver());
xslct.Load(new XPathDocument(XmlReader.Create(new StreamReader(_data))), settings, new XmlUrlResolver());
break;
case XslModes.XsltCompiledDll:
#if FEATURE_COMPILED_XSL
Expand Down

0 comments on commit 6943a55

Please sign in to comment.