152

I want to change my code from:

string path = @"c:\Directory\test.xml";
XmlSerializer s = new XmlSerializer(typeof(Car));

TextReader r = new StreamReader(path);

Car car = (Car)s.Deserialize(r);
r.Close();

into code that would convert an XML to a string, and then convert string to the object Car.

Is this possible?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
user278618
  • 19,306
  • 42
  • 126
  • 196

5 Answers5

287
public static string XmlSerializeToString(this object objectInstance)
{
    var serializer = new XmlSerializer(objectInstance.GetType());
    var sb = new StringBuilder();

    using (TextWriter writer = new StringWriter(sb))
    {
        serializer.Serialize(writer, objectInstance);
    }

    return sb.ToString();
}

public static T XmlDeserializeFromString<T>(this string objectData)
{
    return (T)XmlDeserializeFromString(objectData, typeof(T));
}

public static object XmlDeserializeFromString(this string objectData, Type type)
{
    var serializer = new XmlSerializer(type);
    object result;

    using (TextReader reader = new StringReader(objectData))
    {
        result = serializer.Deserialize(reader);
    }

    return result;
}

To use it:

//Make XML
var settings = new ObjectCustomerSettings();
var xmlString = settings.XmlSerializeToString();

//Make Object
var settings = xmlString.XmlDeserializeFromString<ObjectCustomerSettings>(); 
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Elmer
  • 9,147
  • 2
  • 48
  • 38
  • 4
    Better: public static T XmlDeserializeFromString(string objectData) { return (T)XmlDeserializeFromString(objectData, typeof(T)); } – Lee Treveil Feb 27 '10 at 15:36
  • 1
    can I remove "this" from public static string XmlSerializeToString(this object objectInstance) ?? – user278618 Feb 27 '10 at 20:21
  • You could remove 'this', but then the method is not an extension method anymore. By making the method an extension method, this is possible: string s = "blah!"; string xml = s.XmlSerializeToString(); note: be sure to reference the namespace of the static class that holds the extension methods in your usings, or the extension methods will not work! The only reason for using 'this' is to make it an extension method, it's completely safe to remove it. – Elmer Mar 02 '10 at 04:58
  • You are missing the "this". in your 2nd method to get it to show as an extention. `public static T XmlDeserializeFromString(this string objectData) { return (T)XmlDeserializeFromString(objectData, typeof(T)); }` I am going to edit the main post. – Omzig Mar 31 '14 at 01:12
93

If you have the XML stored inside a string variable you could use a StringReader:

var xml = @"<car/>";
var serializer = new XmlSerializer(typeof(Car));
using (var reader = new StringReader(xml))
{
    var car = (Car)serializer.Deserialize(reader);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    using(var reader = new StringReader(xml)) return (Car)new XmlSerializer(typeof(Car)).Deserialize(reader); – Aelphaeis May 29 '14 at 15:07
17

1-liner, takes a XML string text and YourType as the expected object type. not very different from other answers, just compressed to 1 line:

var result =  (YourType)new XmlSerializer(typeof(YourType)).Deserialize(new StringReader(text));
Toolkit
  • 10,779
  • 8
  • 59
  • 68
guest0815
  • 179
  • 1
  • 3
8
static T DeserializeXml<T>(string sourceXML) where T : class
{
    var serializer = new XmlSerializer(typeof(T));
    T result = null;

    using (TextReader reader = new StringReader(sourceXML))
    {
        result = (T) serializer.Deserialize(reader);
    }

    return result;
}
Yaman
  • 1,030
  • 17
  • 33
6

Shamelessly copied from Generic deserialization of an xml string

    public static T DeserializeFromXmlString<T>(string xmlString)
    {
        var serializer = new XmlSerializer(typeof(T));
        using (TextReader reader = new StringReader(xmlString))
        {
            return (T) serializer.Deserialize(reader);
        }
    }
Community
  • 1
  • 1
Reddy
  • 119
  • 2
  • 2