---
title: "About Reflector, Encoding, And an Article Error"
url: "https://osherove.com/blog/2003/6/13/about-reflector-encoding-and-an-article-error.md"
canonical: "https://osherove.com/blog/2003/6/13/about-reflector-encoding-and-an-article-error.html"
published: "2003-06-13T21:27:00"
updated: "2003-06-13T21:27:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "Yes. Lutz Roeder's Reflector is one cool tool. I just found out using it that my latest article has some bad code in it because of reflector. The article mentions that in order to write to a memory buffer instead f a…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# About Reflector, Encoding, And an Article Error

By Roy Osherove June 13, 2003 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published June 13, 2003; technical details may be outdated.

Yes. Lutz Roeder's [Reflector](http://www.aisto.com/roeder/dotnet/) is one cool tool. I just found out using it that my [latest article](http://www.ondotnet.com/pub/a/dotnet/2003/06/09/parsinglogs.html) has some bad code in it because of reflector.

The article mentions that in order to write to a memory buffer instead f a file you can initialize a new _XMLTextWriter_ object using a _StringWriter_ object(which writes to a _StringBuilder_ object in turn). In the article ,I discovered a problem that when writing to a string buffer using the _StringWriter object initializer_, the XML document that is created always turns out with an Encoding header of **UTF-16** at the top. I couldn't figure out why.

I assumed it was something on my own system's configuration , so I pointed to a workaround for the problem - make the _XMLTextWriter_ write directly to a file using a different constructor. That constructor allows you to specify the desired _Encoding_ for that file, thus allowing you to specify **UTF-8**. I then wrote in the article that "You _**might**_ get a UTF-16 encoding header" but I was wrong.

A quick peek inside the _StringWriter_'s constructor using [Reflector](http://www.aisto.com/roeder/dotnet/) revealed the awful truth - The _StringWriter_ initializes itself with a new _UnicodeEncoding_ object by default, with no constructor available to change this decision. More so, the Encoding property of the _StringWriter_ is read-only, thus you can't change it after creation.

So, this means (or am I way off base here) that whenever you'd use a _StringWriter_ for an _XMLTextWriter_ ctor, you would get a **UTF-16** XML header. **Always**. No way to change that. That means you can't write an XML Document to a memory buffer  as a UTF-8 encoded XML without going through some hoops.

Sure, I could use other classes in the framework but I shouldn't have to. Why should the Encoding property of the StringWriter be Read-Only?

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Jun 15, 2003

    ### [Data Binding Woes - A Simple Solution](https://osherove.com/blog/2003/6/15/data-binding-woes-a-simple-solution.html)

- Jun 14, 2003

    ### [Winforms Data Binding Woes](https://osherove.com/blog/2003/6/14/winforms-data-binding-woes.html)

- Jun 12, 2003

    ### [AppDomains Reloaded](https://osherove.com/blog/2003/6/12/appdomains-reloaded.html)

- Jun 12, 2003

    ### [.Net Debugging Resources Grows](https://osherove.com/blog/2003/6/12/net-debugging-resources-grows.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
