HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Object-to-XML string implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementationobjectstringxml

Problem

I have recently implemented a code to accomplish Object-to-String. Can you suggest any pros and cons with regard to the code?

public static string ObjectToXml(object obj)
{
    var oXmlSerializer = new XmlSerializer(obj.GetType());
    var oStringWriter = new StringWriter();
    var oXmlSerializerNamespaces = new XmlSerializerNamespaces();
    oXmlSerializerNamespaces.Add(string.Empty,string.Empty);
    var oXmlWriterSettings = new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = false,
        Encoding = Encoding.GetEncoding("ISO-8859-1")
    };

    var oXmlWriter = XmlWriter.Create(oStringWriter,oXmlWriterSettings);
    oXmlSerializer.Serialize(oXmlWriter, obj, oXmlSerializerNamespaces);
    oXmlWriter.Close();
    return oStringWriter.ToString();
}

Solution

-
StringWriter and XmlWriter are both IDisposable hence their usage should be wrapped in a using statement

-
The prefixing o of your local variables smells a bit of hungarian notation which generally does not convey a lot of useful information.

-
The encoding has already been mentioned in the comments. In .NET strings are UTF-16 so you use should that instead.

-
Generally try to declare your variables as close to the point of use as possible. That way it's more obvious from which point on they are being used.

Refactored code:

public static string ObjectToXml(object obj)
{
    var settings = new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = false,
        Encoding = Encoding.GetEncoding("UTF-16")
    };

    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty,string.Empty);

    var serializer = new XmlSerializer(obj.GetType());

    using (var stringWriter = new StringWriter())
    {
        using (var xmlWriter = XmlWriter.Create(stringWriter, settings)
        {
            serializer.Serialize(xmlWriter, obj, namespaces);
        }
        return stringWriter.ToString();
    }
}


Update: As pointed out in the comments the suggested refactored code violates point 4 above. This is because I usually try to limit the scope of using blocks so it's easier to see what's going on there.

As pointed out by Jesse you could make some of the local variables class static as they are always the same. If you put generics in the mix then you could also make the serializer class static and the total refactored code could look like this:

public static class SerializerHelper 
{
    private static XmlSerializer _Serializer;
    private static XmlWriterSettings _XmlSettings;

    private static XmlSerializerNamespaces _Namespaces;

    static SerializerHelper()
    {
        _Namespaces = new XmlSerializerNamespaces();
        _Namespaces.Add(string.Empty,string.Empty);

        _XmlSettings = new XmlWriterSettings
            {
                Indent = true,
                OmitXmlDeclaration = false,
                Encoding = Encoding.GetEncoding("ISO-8859-1")
            };

        _Serializer = new XmlSerializer(typeof(T));
    }

    public static string ObjectToXml(T obj)
    {
        using (var stringWriter = new StringWriter())
        {
            using (var xmlWriter = XmlWriter.Create(stringWriter, _XmlSettings))
            {
                _Serializer.Serialize(xmlWriter, obj, _Namespaces);
            }
            return stringWriter.ToString();
        }
    }
}

Code Snippets

public static string ObjectToXml(object obj)
{
    var settings = new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = false,
        Encoding = Encoding.GetEncoding("UTF-16")
    };

    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty,string.Empty);

    var serializer = new XmlSerializer(obj.GetType());

    using (var stringWriter = new StringWriter())
    {
        using (var xmlWriter = XmlWriter.Create(stringWriter, settings)
        {
            serializer.Serialize(xmlWriter, obj, namespaces);
        }
        return stringWriter.ToString();
    }
}
public static class SerializerHelper<T> 
{
    private static XmlSerializer _Serializer;
    private static XmlWriterSettings _XmlSettings;

    private static XmlSerializerNamespaces _Namespaces;

    static SerializerHelper()
    {
        _Namespaces = new XmlSerializerNamespaces();
        _Namespaces.Add(string.Empty,string.Empty);

        _XmlSettings = new XmlWriterSettings
            {
                Indent = true,
                OmitXmlDeclaration = false,
                Encoding = Encoding.GetEncoding("ISO-8859-1")
            };

        _Serializer = new XmlSerializer(typeof(T));
    }

    public static string ObjectToXml(T obj)
    {
        using (var stringWriter = new StringWriter())
        {
            using (var xmlWriter = XmlWriter.Create(stringWriter, _XmlSettings))
            {
                _Serializer.Serialize(xmlWriter, obj, _Namespaces);
            }
            return stringWriter.ToString();
        }
    }
}

Context

StackExchange Code Review Q#64029, answer score: 11

Revisions (0)

No revisions yet.