Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an object to an XML string

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.

like image 803
FluepkeSchaeng Avatar asked Jul 12 '12 08:07

FluepkeSchaeng


People also ask

How to convert object to xml?

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.


2 Answers

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 ;         }     } 
like image 58
Tomas Grosup Avatar answered Oct 14 '22 00:10

Tomas Grosup


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.

like image 37
William Smith Avatar answered Oct 14 '22 00:10

William Smith