Skip to content

Copilot/breakdown streamed output methods - #1

Open
jtenos wants to merge 6 commits into
mainfrom
copilot/breakdown-streamed-output-methods
Open

Copilot/breakdown streamed output methods#1
jtenos wants to merge 6 commits into
mainfrom
copilot/breakdown-streamed-output-methods

Conversation

@jtenos

@jtenos jtenos commented Jun 6, 2026

Copy link
Copy Markdown
Owner

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

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.

Copilot AI and others added 6 commits December 10, 2025 06:22
…) 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>
@jtenos jtenos linked an issue Jun 6, 2026 that may be closed by this pull request
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.

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

2 participants