I have a three classes that are serialized into XML data via XMLserializer Method. Is there a way to make the method accessible by all of them rather than have three xmlserialize methods in my code.
//Here I'm writing to my class with the data that needs to be serialized
PERSON r = new PERSON();
Input l = new Search.Input();
INPUTSEARCHNBR ln = new INPUTSEARCHNBR();
ln.number = pnumber;
r.query = ln;
string xml = doSerialization(r);
//Call the xmlserializer method
private static string doSerialization(PERSON r)
{
string serializedValue = string.Empty;
try
{
XmlSerializer xmlSerializer = new XmlSerializer(r.GetType());
using (MemoryStream ms = new MemoryStream())
{
xmlSerializer.Serialize(ms, r);
ms.Position = 0;
var sr = new StreamReader(ms);
serializedValue = sr.ReadToEnd();
}
serializedVal = serializedValue.Replace("version=\"0\"", string.Empty).Replace("<?xml version=\"1.0\"?>", string.Empty).Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", string.Empty);
}
return serializedVal;
}
My code repeats this two more time for two different classes, it writes to the class and then calls another xmlserialize method
because the input of the method is a different class type.
Is there are a way to make the method public or have as a class or any other way so that I don’t have to paste two additional
xmlserialize method that are exactly the same but the input class is different.
Since you don’t do anything that is class specific, you could type
object: