Reputation: 65195
Problem with the code below, on this line:
XmlSerializer x = new XmlSerializer(typeof(????));
I want the type to be define by the record object, can I do that?
public void ConvertRecordObjectToXML(object RecordObject, out string XML)
{
string xml = string.Empty;
try
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer x = new XmlSerializer(typeof(????)); // <---- ?????
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
x.Serialize(xmlTextWriter, RecordObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
XML = XmlizedString;
}
catch (Exception e)
{
System.Console.WriteLine(e);
xml = "";
}
}
Upvotes: 0
Views: 4032
Reputation: 501
Every object inherits the method GetType() from Object, so you should be able to use:
XmlSerializer x = new XmlSerializer(RecordObject.GetType());
The C# typeof keyword takes the class name and yields an object of type Type. GetType() yields the same thing, but operates on an instance instead.
Upvotes: 7
Reputation: 50235
Have you tried this?
XmlSerializer x = new XmlSerializer(RecordObject.GetType());
Upvotes: 3