Reputation: 33
I'm attempting to setup my first export/import of a list to/from an external XML file. Upon start I have the following code:
private void Form1_Load(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Directory.Exists("C:\\Temp"))
Directory.CreateDirectory("C:\\Temp");
if (!File.Exists("C:\\Temp\\mygames.xml"))
File.Create("C:\\Temp\\mygames.xml").Close();
//Call method to read data from XML to List<>
videogame = ReadFromXmlFile<List<vglist>>("C:\\Temp\\mygames.xml");
displayList();
} // END of method Form1_Load
With the ReadFromXmlFile method as follows:
public static T ReadFromXmlFile<T>(string filePath) where T : new()
{
TextReader reader = null;
try
{
var serializer = new XmlSerializer(typeof(T));
reader = new StreamReader(filePath);
return (T)serializer.Deserialize(reader);
} // end try
finally
{
if (reader != null)
reader.Close();
} // end finally
} // END ReadFromXmlFile<T> method
But I keep getting the following error upon execution of the program:
Exception thrown: 'System.InvalidOperationException' in System.Xml.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
Additional information: There is an error in XML document (0, 0).
My class is set up as follows:
public class vglist
{
[XmlElement("gameName")] // XML tag
public string GameName // game field
{
get;
set;
}
etc...
Any ideas? I've search everywhere with similar problems but no solutions which are helping.
Upvotes: 0
Views: 121
Reputation: 133
Looks like if the file does not exist, your creating a new blank XML file. I suspect your error is due to the XML file containing nothing.
Upvotes: 1