Still.Tony
Still.Tony

Reputation: 1434

XmlWriter writing empty stringbuilder

I had the following code which "worked" for my testing but I didn't like the formatting:

System.Diagnostics.Debug.WriteLine("----------------------------- Start 1 ----------------------------");
using (var sw = new Utf8StringWriter())
//StringBuilder sb = new StringBuilder();
//using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    XDocument d = new XDocument(new XDeclaration(version: "1.0", encoding: "UTF-8", standalone: "no"));
    XElement DMTStandardIF = new XElement("DMTStandardIF", new XAttribute("version", "1.00"));
    d.Add(DMTStandardIF);
    XElement last = DMTStandardIF;
    last.Add(last = new XElement("Thing1", "thing 1 stuff"));
    last.Add(null);
    last.Add(last = new XElement("Thing2", "thing 2 stuff"));
    last.Add(null);
    d.Save(sw);
    //d.WriteTo(xw);
    System.Diagnostics.Debug.WriteLine(sw);
    //System.Diagnostics.Debug.WriteLine(sb.ToString());
}

// So I changed it to the following which outputs nothing; xw has content but sb is empty.

System.Diagnostics.Debug.WriteLine("----------------------------- Start 2 ----------------------------");
StringBuilder sb = new StringBuilder();
using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    XDocument d = new XDocument(new XDeclaration(version: "1.0", encoding: "UTF-8", standalone: "no"));
    XElement DMTStandardIF = new XElement("DMTStandardIF", new XAttribute("version", "1.00"));
    d.Add(DMTStandardIF);
    XElement last = DMTStandardIF;
    last.Add(last = new XElement("Thing1", "thing 1 stuff"));
    last.Add(null);
    last.Add(last = new XElement("Thing2", "thing 2 stuff"));
    last.Add(null);
    d.WriteTo(xw);
    //d.Save(xw); // I tried this too and .. empty as well
    System.Diagnostics.Debug.WriteLine(sb.ToString());
}
System.Diagnostics.Debug.WriteLine("------------------------------ Done ------------------------------");

Here's what you'll get if you run that:

----------------------------- Start 1 ----------------------------
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<DMTStandardIF version="1.00">
  <Thing1>thing 1 stuff<Thing2>thing 2 stuff</Thing2></Thing1>
</DMTStandardIF>
----------------------------- Start 2 ----------------------------

------------------------------ Done ------------------------------

WHY does the second version output nothing? How do I fix it?

This all started because I wanted to test what happens if I attempt myXElement.Add(null) and I already answered that question but I saw XmlWriter writing empty down the rabbit hole and went after it.

Upvotes: 1

Views: 441

Answers (1)

Steve
Steve

Reputation: 216293

Your code doesn't print anything because when you call the Debug.WriteLine the StringBuilder has not yet received the buffer from the XmlWriter. This happens (probably with a call to xw.Flush()) when your code exits from the using statement

Just move the print of the StringBuilder outside the using statement

using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    ....
    d.WriteTo(xw);
}
System.Diagnostics.Debug.WriteLine(sb.ToString());
System.Diagnostics.Debug.WriteLine("------------------------------ Done --------------------");

In alternative you can force a Flush on the XmlWriter inside the using statement just before printing the StringBuilder.

using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    ....
    d.WriteTo(xw);
    xw.Flush();
    System.Diagnostics.Debug.WriteLine(sb.ToString());
}
System.Diagnostics.Debug.WriteLine("------------------------------ Done --------------------");

But in this context it seems pointless and redundant (it will be called again) considering that the code exits the using statement just on the next line.

Upvotes: 2

Related Questions