Quick question involving xml deserialization and strings
-
Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?
-
Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?
-
Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?
I don't think there's a smart way of doing this. You could allocate a byte array, create a MemoryStream on it, and then write your string to the memory stream. Then you can deserialize from that.
using System.Beer;
-
Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?
Off the top of my head, you could try this:
public T FromString<T>(string serializedObject) where T : new() { XmlSerializer xmlSer = new XmlSerializer(typeof(T)); StringReader sr = new StringReader(serializedObject); XmlTextReader stream = new XmlTextReader(sr); try { T obj = (T)xmlSer.Deserialize(stream); return obj; } catch(Exception ex) { // Log ex... } finally { sr.Close(); stream.Close(); } }
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.
-
Off the top of my head, you could try this:
public T FromString<T>(string serializedObject) where T : new() { XmlSerializer xmlSer = new XmlSerializer(typeof(T)); StringReader sr = new StringReader(serializedObject); XmlTextReader stream = new XmlTextReader(sr); try { T obj = (T)xmlSer.Deserialize(stream); return obj; } catch(Exception ex) { // Log ex... } finally { sr.Close(); stream.Close(); } }
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.
-
Glad to help.:-D
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.
-
I don't think there's a smart way of doing this. You could allocate a byte array, create a MemoryStream on it, and then write your string to the memory stream. Then you can deserialize from that.
using System.Beer;