I get an error below when an xml file is blank or empty. What to do in this case????
Error Received: There is an error in XML Document (0,0)
XmlSerializer serializer = new XmlSerializer(typeof(Colors));
TextReader textReader = new StreamReader(colorName);
colorfile = (Colors)serializer.Deserialize(textReader); Error on this line of code, What to do for this case when the file is empty??? I want to to be able to still save data to the file.
textReader .Close();
When deserializing the object is the file is empty and I get a document error. I am assuming the because do not the xml document header attributes,
Loading
VulpesPosted Jul 26, 2013, 4:53 AM
David SmithPosted Jul 26, 2013, 1:42 AM
David SmithPosted Jul 25, 2013, 10:06 PM
VulpesPosted Jul 25, 2013, 7:09 PM
Its InnerException property gives more details.
For example, in the case of an empty document, it gives a message of:
'Root element is missing'.
You could use this to pin down the situation more tightly but the bottom line is that the file can't be deserialized as it stands and, unless there's a possibility of repairing it manually, then maybe the best strategy is to replace it anyway.
David SmithPosted Jul 25, 2013, 6:48 PM
David SmithPosted Jul 25, 2013, 5:46 PM
VulpesPosted Jul 25, 2013, 5:46 PM
David SmithPosted Jul 25, 2013, 5:41 PM
VulpesPosted Jul 25, 2013, 4:27 PM
What I'd suggest is that you catch the exception if the file is blank and then just serialize a basic object to the file so it will be a valid XML document. The code would be something like this:
David SmithPosted Jul 25, 2013, 3:52 PM
Then I deleted all the content in the file to see what would happen and I got this error.
So I was thinking what if someone create a blank file and name the file with an .xml extension.
How to handle this situation. I was thinking the file is black we add the root elements somehow.
VulpesPosted Jul 25, 2013, 3:11 PM
I assume you can't be creating a new file as there wouldn't then be any point in deserializing it.
David SmithPosted Jul 25, 2013, 12:52 PM
Sunny SharmaPosted Jul 25, 2013, 12:47 PM