Skip to content

Commit 6a2d4ac

Browse files
authored
add XDocument.SaveAsync (#196)
1 parent 548be70 commit 6a2d4ac

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

apiCount.include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**API count: 299**
1+
**API count: 302**

api_list.include.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@
380380
* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat)
381381

382382

383+
#### Xml.Linq.XDocument
384+
385+
* `Task SaveAsync(Xml.XmlWriter, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.saveasync#system-xml-linq-xdocument-saveasync(system-xml-xmlwriter-system-threading-cancellationtoken))
386+
* `Task SaveAsync(Stream, Xml.Linq.SaveOptions, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.saveasync#system-xml-linq-xdocument-saveasync(system-io-stream-system-xml-linq-saveoptions-system-threading-cancellationtoken))
387+
* `Task SaveAsync(TextWriter, Xml.Linq.SaveOptions, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.saveasync#system-xml-linq-xdocument-saveasync(system-io-textwriter-system-xml-linq-saveoptions-system-threading-cancellationtoken))
388+
389+
383390
### Static helpers
384391

385392
#### EnumPolyfill

src/Consume/Consume.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
using System.Text.RegularExpressions;
1919
using System.Threading;
2020
using System.Threading.Tasks;
21+
using System.Xml;
22+
using System.Xml.Linq;
23+
using MemoryStream = System.IO.MemoryStream;
24+
2125
#pragma warning disable CS4014
2226

2327
class Consume
@@ -457,6 +461,14 @@ void GetMemberWithSameMetadataDefinitionAs(MemberInfo info)
457461
var result = typeof(string).GetMemberWithSameMetadataDefinitionAs(info);
458462
}
459463

464+
void XDocumentSaveAsync()
465+
{
466+
var document = new XDocument();
467+
document.SaveAsync(new XmlTextWriter(null!), CancellationToken.None);
468+
document.SaveAsync(new StringWriter(), SaveOptions.None, CancellationToken.None);
469+
document.SaveAsync(new MemoryStream(), SaveOptions.None, CancellationToken.None);
470+
}
471+
460472
#if FeatureMemory
461473
void RandomNextBytesSpan()
462474
{

src/Polyfill/Polyfill_XDocument.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
#if NETFRAMEWORK || NETSTANDARD2_0
5+
6+
using System;
7+
using System.Diagnostics.CodeAnalysis;
8+
using System.Xml;
9+
using System.IO;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
using System.Xml.Linq;
13+
using Link = System.ComponentModel.DescriptionAttribute;
14+
15+
static partial class Polyfill
16+
{
17+
/// <summary>
18+
/// Output this <see cref="XDocument"/> to an <see cref="XmlWriter"/>.
19+
/// </summary>
20+
/// <param name="writer">
21+
/// The <see cref="XmlWriter"/> to output the XML to.
22+
/// </param>
23+
/// <param name="cancellationToken">
24+
/// A cancellation token.
25+
/// </param>
26+
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.saveasync#system-xml-linq-xdocument-saveasync(system-xml-xmlwriter-system-threading-cancellationtoken)")]
27+
public static Task SaveAsync(
28+
this XDocument target,
29+
XmlWriter writer,
30+
CancellationToken cancellationToken)
31+
{
32+
cancellationToken.ThrowIfCancellationRequested();
33+
target.Save(writer);
34+
return Task.CompletedTask;
35+
}
36+
37+
/// <summary>
38+
/// Output this <see cref="XDocument"/> to a <see cref="Stream"/>.
39+
/// </summary>
40+
/// <param name="stream">
41+
/// The <see cref="Stream"/> to output the XML to.
42+
/// </param>
43+
/// <param name="options">
44+
/// If SaveOptions.DisableFormatting is enabled the output is not indented.
45+
/// If SaveOptions.OmitDuplicateNamespaces is enabled duplicate namespace declarations will be removed.
46+
/// </param>
47+
/// <param name="cancellationToken">A cancellation token.</param>
48+
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.saveasync#system-xml-linq-xdocument-saveasync(system-io-stream-system-xml-linq-saveoptions-system-threading-cancellationtoken)")]
49+
public static Task SaveAsync(
50+
this XDocument target,
51+
Stream stream,
52+
SaveOptions options,
53+
CancellationToken cancellationToken)
54+
{
55+
cancellationToken.ThrowIfCancellationRequested();
56+
target.Save(stream, options);
57+
return Task.CompletedTask;
58+
}
59+
60+
/// <summary>
61+
/// Output this <see cref="XDocument"/> to a <see cref="TextWriter"/>.
62+
/// </summary>
63+
/// <param name="textWriter">
64+
/// The <see cref="TextWriter"/> to output the XML to.
65+
/// </param>
66+
/// <param name="options">
67+
/// If SaveOptions.DisableFormatting is enabled the output is not indented.
68+
/// If SaveOptions.OmitDuplicateNamespaces is enabled duplicate namespace declarations will be removed.
69+
/// </param>
70+
/// <param name="cancellationToken">A cancellation token.</param>
71+
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.saveasync#system-xml-linq-xdocument-saveasync(system-io-textwriter-system-xml-linq-saveoptions-system-threading-cancellationtoken)")]
72+
public static Task SaveAsync(
73+
this XDocument target,
74+
TextWriter textWriter,
75+
SaveOptions options,
76+
CancellationToken cancellationToken)
77+
{
78+
cancellationToken.ThrowIfCancellationRequested();
79+
target.Save(textWriter, options);
80+
return Task.CompletedTask;
81+
}
82+
}
83+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Xml;
2+
using System.Xml.Linq;
3+
4+
partial class PolyfillTests
5+
{
6+
[Test]
7+
public async Task XDocumentSaveAsyncXmlWriter()
8+
{
9+
var document = GetXDocument();
10+
var builder = new StringBuilder();
11+
var settings = new XmlWriterSettings
12+
{
13+
OmitXmlDeclaration = true,
14+
Indent = true,
15+
Async = true
16+
};
17+
18+
using var writer = XmlWriter.Create(builder, settings);
19+
await document.SaveAsync(writer, Cancel.None);
20+
await writer.FlushAsync();
21+
Assert.AreEqual("<Child />", builder.ToString());
22+
}
23+
24+
[Test]
25+
public async Task XDocumentSaveAsyncStream()
26+
{
27+
var document = GetXDocument();
28+
using var stream = new MemoryStream();
29+
30+
await document.SaveAsync(stream, SaveOptions.DisableFormatting, Cancel.None);
31+
stream.Position = 0;
32+
using var reader = new StreamReader(stream);
33+
var result = await reader.ReadToEndAsync();
34+
35+
Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-8\"?><Child />", result);
36+
}
37+
38+
static XDocument GetXDocument() =>
39+
new(new XElement("Child"));
40+
}

0 commit comments

Comments
 (0)