I have got a class named WebserviceType
I got from the tool xsd.exe from an XSD file.
Now I want to deserialize an instance of an WebServiceType
object to a string. How can I do this?
The MethodCheckType
object has as params a WebServiceType
array.
My first try was like I serialized it: with a XmlSerializer
and a StringWriter
(while serializing I used a StringReader
).
This is the method in which I serialize the WebServiceType
object:
XmlSerializer serializer = new XmlSerializer(typeof(MethodCheckType)); MethodCheckType output = null; StringReader reader = null; // catch global exception, logg it and throw it try { reader = new StringReader(path); output = (MethodCheckType)serializer.Deserialize(reader); } catch (Exception) { throw; } finally { reader.Dispose(); } return output.WebService;
Edit:
Maybe I could say it in different words: I have got an instance of this MethodCheckType
object an on the other hand I have got the XML document from which I serialized this object. Now I want to convert this instance into a XML document in form of a string. After this I have to proof if both strings (of XML documents) are the same. This I have to do, because I make unit tests of the first method in which I read an XML document into a StringReader
and serialize it into a MethodCheckType
object.
To be able to convert an object in C# to XML, we will make use of a function called XmlSerializer() function which serializes the given object in C# to XML format and another function called XmlTextWriter() function to output the serialized XML string.
Here are conversion method for both ways. this = instance of your class
public string ToXML() { using(var stringwriter = new System.IO.StringWriter()) { var serializer = new XmlSerializer(this.GetType()); serializer.Serialize(stringwriter, this); return stringwriter.ToString(); } } public static YourClass LoadFromXMLString(string xmlText) { using(var stringReader = new System.IO.StringReader(xmlText)) { var serializer = new XmlSerializer(typeof(YourClass )); return serializer.Deserialize(stringReader) as YourClass ; } }
I realize this is a very old post, but after looking at L.B's response I thought about how I could improve upon the accepted answer and make it generic for my own application. Here's what I came up with:
public static string Serialize<T>(T dataToSerialize) { try { var stringwriter = new System.IO.StringWriter(); var serializer = new XmlSerializer(typeof(T)); serializer.Serialize(stringwriter, dataToSerialize); return stringwriter.ToString(); } catch { throw; } } public static T Deserialize<T>(string xmlText) { try { var stringReader = new System.IO.StringReader(xmlText); var serializer = new XmlSerializer(typeof(T)); return (T)serializer.Deserialize(stringReader); } catch { throw; } }
These methods can now be placed in a static helper class, which means no code duplication to every class that needs to be serialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With