Skip to content

Commit 63640f2

Browse files
committed
Use ReadToEndAsync in LoadAsync polyfills
Mark XDocument/XElement.LoadAsync as async and read the provided TextReader using ReadToEndAsync, then call XDocument.Load/XElement.Load on the resulting string. This replaces the previous Task.FromResult of the synchronous Load(textReader, ...) to avoid blocking on reader IO and enable asynchronous reading; cancellation is still checked at the start of the methods.
1 parent 0092bd3 commit 63640f2

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Polyfill/XDocumentPolyfill.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ public static Task<XDocument> LoadAsync(Stream stream, LoadOptions options, Canc
3838
/// Asynchronously creates a new XDocument and initializes its underlying XML tree using the specified text reader, optionally preserving white space.
3939
/// </summary>
4040
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.loadasync?view=net-11.0#system-xml-linq-xdocument-loadasync(system-io-textreader-system-xml-linq-loadoptions-system-threading-cancellationtoken)
41-
public static Task<XDocument> LoadAsync(TextReader textReader, LoadOptions options, CancellationToken cancellationToken)
41+
public static async Task<XDocument> LoadAsync(TextReader textReader, LoadOptions options, CancellationToken cancellationToken)
4242
{
4343
cancellationToken.ThrowIfCancellationRequested();
44-
return Task.FromResult(XDocument.Load(textReader, options));
44+
var content = await textReader.ReadToEndAsync();
45+
return XDocument.Load(content, options);
4546
}
4647

4748
/// <summary>

src/Polyfill/XElementPolyfill.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ public static Task<XElement> LoadAsync(Stream stream, LoadOptions options, Cance
3838
/// Asynchronously creates a new XElement and initializes its underlying XML tree using the specified text reader, optionally preserving white space.
3939
/// </summary>
4040
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xelement.loadasync?view=net-11.0#system-xml-linq-xelement-loadasync(system-io-textreader-system-xml-linq-loadoptions-system-threading-cancellationtoken)
41-
public static Task<XElement> LoadAsync(TextReader textReader, LoadOptions options, CancellationToken cancellationToken)
41+
public static async Task<XElement> LoadAsync(TextReader textReader, LoadOptions options, CancellationToken cancellationToken)
4242
{
4343
cancellationToken.ThrowIfCancellationRequested();
44-
return Task.FromResult(XElement.Load(textReader, options));
44+
var content = await textReader.ReadToEndAsync();
45+
return XElement.Load(content, options);
4546
}
4647

4748
/// <summary>

0 commit comments

Comments
 (0)