The Coolness Of Inheritance

I was writing a simple request/response to a non-WCF web service.  The service’s request SOAP looked like this:

<?xml version="1.0" encoding="utf-8" ?>
<WorkItem AppName='TBD'>
  <UserName nametype='familiar'>Jamie</UserName>

created some classes that matched the request:

public class WorkItem
    public string ApplicationName { get; set; }
    public UserName UserName { get; set; }
    public string UserItem { get; set; }


public class UserName
    [XmlAttribute(AttributeName = "nameType")] 
    public string NameType { get; set; }
    public string Value { get; set; }

I then created a function that populates these classes with the data:

static WorkItem CreateWorkItem()
    WorkItem workItem = new WorkItem();
    UserName userName = new UserName();

    userName.NameType = "familiar";
    userName.Value = "Jamie";

    workItem.ApplicationName = "TBD";
    workItem.UserName = userName;
    workItem.UserItem = "Pencil";

    return workItem;

Finally, I created a helper function that takes the classes and serializes them as XML:

static XmlDocument CreateXMLDocument(WorkItem workItem)

    XmlSerializer serializer = new XmlSerializer(typeof(WorkItem));
    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
    namespaces.Add(String.Empty, String.Empty);
    StringWriter stringWriter = new StringWriter();
    serializer.Serialize(stringWriter, workItem, namespaces);

    XmlDocument xmlDocument = new XmlDocument();
    return xmlDocument;

When I run it, things look great… except that the Encoding is wrong:


The path of least resistance would be to set the Encoding property of the StringWriter class.  However, that property is read-only.  After playing around with the different classes in System.IO that expose encoding (usually though the constructor), I stumbled upon this great article.  The easiest way to get UTF-8 encoding in a stringWriter is to override the default implementation in the constructor.  I went ahead and created a new class and overrode the Encoding property.

public class UTF8StringWriter : StringWriter
    Encoding encoding;
    public UTF8StringWriter()
        : base()
        this.encoding = Encoding.UTF8;

    public override Encoding Encoding
            return encoding;

Note that I used a local variable.  Thank goodness the StringWriter uses its property (not a private variable) in the Serialize method.  A big thank you to whoever wrote that class in a proper way.  I then changed the stringWriter variable to a UTF8WringWriter like this:

UTF8StringWriter stringWriter = new UTF8StringWriter();

The output now renders correctly:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: