Skip to content

Add stream-based API and WriteToString() methods to FluentXmlWriter#15

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/breakdown-streamed-output-methods
Draft

Add stream-based API and WriteToString() methods to FluentXmlWriter#15
Copilot wants to merge 6 commits intomainfrom
copilot/breakdown-streamed-output-methods

Conversation

Copy link
Contributor

Copilot AI commented Dec 10, 2025

Implements two distinct usage patterns: stream-based writing with explicit completion via Done(), and WriteToString() methods as cleaner alternatives to OutputToString().

Changes

Stream-based API

  • Start(TextWriter, topLevelElement, FormattingOptions?) - writes directly to provided writer
  • Start(Stream, topLevelElement, FormattingOptions?, Encoding?) - creates internal StreamWriter
  • Done() - flushes and disposes stream (only disposes when we own the TextWriter)
  • Prevents calling OutputToString()/OutputToFile() in stream mode
  • Prevents multiple Done() calls with _isDone flag

WriteToString() methods

  • WriteToString(), WriteToString(bool), WriteToString(FormattingOptions)
  • Delegates to existing OutputToString() implementation

Internal changes

  • CustomXmlWriter accepts any TextWriter (not just StringWriter)
  • _isStreamMode and _ownsTextWriter flags control behavior and disposal

Examples

Stream-based writing:

using (var fileStream = File.Create("output.xml"))
{
    FluentXmlWriter.Start(fileStream, "root", FormattingOptions.Default.WithSpaces(2))
        .ManySimple(
            SimpleElement.Create("foo").Attr("id", "1"),
            SimpleElement.Create("bar").Attr("id", "2")
        )
        .Done(); // Flushes and disposes
}

String output:

string xml = FluentXmlWriter.Start("root")
    .Complex("child").Text("value").EndElem()
    .WriteToString(indented: true);

All existing API methods remain unchanged and functional. Added 16 tests covering new functionality.

Original prompt

This section details on the original issue you should resolve

<issue_title>Break down into a streamed version and one that outputs</issue_title>
<issue_description>Have two paths that use the same basic structure.

For the first one, accept a stream in the Start method, and make a Done() method which flushes and disposees the underlying stream:

FluentXmlWriter.Start(File.CreateText(@"/filename.txt"), FormattingOptions.Default, Encoding.UTF8)
    .ManySimple(
        SimpleElement.Create("foo").Attr("id", "1"),
        SimpleElement.Create("bar").Attr("id", "2")
    ).Done(); // Done() flushes and disposes the file stream

For the second one, keep the OutputTo... methods. Instead of a StringBuilder/StringWriter, use a MemoryStream so that you can use the same underlying technology as the first one.

string xml = FluentXmlWriter.Start(File.CreateText(@"/filename.txt"), FormattingOptions.Default, Encoding.UTF8)
    .ManySimple(
        SimpleElement.Create("foo").Attr("id", "1"),
        SimpleElement.Create("bar").Attr("id", "2")
    ).WriteToString(); // Flushes the underlying memory stream and returns the output as a string.
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits December 10, 2025 06:29
…) methods

Co-authored-by: jtenos <607782+jtenos@users.noreply.github.com>
…t multiple Done() calls

Co-authored-by: jtenos <607782+jtenos@users.noreply.github.com>
…t internally

Co-authored-by: jtenos <607782+jtenos@users.noreply.github.com>
Co-authored-by: jtenos <607782+jtenos@users.noreply.github.com>
Co-authored-by: jtenos <607782+jtenos@users.noreply.github.com>
Copilot AI changed the title [WIP] Break down functionality into streamed and output versions Add stream-based API and WriteToString() methods to FluentXmlWriter Dec 10, 2025
Copilot AI requested a review from jtenos December 10, 2025 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Break down into a streamed version and one that outputs

2 participants