I need to make this class as generic as possible... I want to pass a XML string Path and return the data of the type of object I pass to the getDataFromFile() Method. Here's what I made so far:
public class XmlFile
{
public string mXmlFilePath { get; set; }
public XmlFile(string xmlFilePath)
{
this.mXmlFilePath = xmlFilePath;
}
public object getDataFromFile(object dataObject)
{
LogFile.addLogEntry("Getting data from XML file.");
object data = null;
Type type = dataObject.GetType();
try
{
StreamReader xmlStream = new StreamReader(this.mXmlFilePath);
XmlSerializer xmlfile = new XmlSerializer(type);
data = (type)xmlfile.Deserialize(xmlStream);
xmlStream.Close();
return data;
}
catch (Exception ex)
{
LogFile.addLogEntry("Error reading data from XML file: " + ex.Message);
return null;
}
}
}
I know I should be using reflection, but I'm having trouble making this cast data = (type)xmlfile.Deserialize(xmlStream);
thank you...
Aucun commentaire:
Enregistrer un commentaire