Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I have an xml File named as History.xml and also corresponding History.vb which contains only two properties namely TItleName and FileName.
The Obj returned is correct. But Community 2015 while debugging, when executing the line following Try immediately proceeds to Throw with an exception in output as follows:
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll

The obj returned contains all objects that are found in history.xml file.

The following code in full is used for deserializing history.xml file.
VB
Public Shared Function InitializeCollectionSource(Of T)(path As String) As ObservableCollection(Of T)
        Dim obj As ObservableCollection(Of T) = New ObservableCollection(Of T)
        If Not File.Exists(path) Then Return obj
        Using reader = New FileStream(path, FileMode.Open)
            Dim serializer = New XmlSerializer(obj.GetType)
            Try
                obj = CType(serializer.Deserialize(reader), ObservableCollection(Of T))
            Catch ex As Exception
                Throw
            Finally
                serializer = Nothing
            End Try
        End Using

        Return obj
    End Function

The following is my questions:
1. Please inform me as to Why exception is thrown.
2. Also please suggest as to What is to be done to correct it.
Thanks.

After Comment:
With due respect, gentlemen, the class that is deserialized has parameterless constructor. Also, tostring, GetHashCode and Equals are implemented. I have stated already, that the initialization works well, ie. object deserialized is complete. Please review your comments.
Posted
Updated 27-Oct-15 17:43pm
v3
Comments
Richard MacCutchan 27-Oct-15 9:43am    
When you catach an exception you should display the details, so you can find out what is wrong with your code.
bojammis 27-Oct-15 11:52am    
I agree with Richard, you need to review the exception message. Some common areas to consider: XML can serialize an object if the object has a parameter-less constructor(this gets me every time). Also properties of other object must have parameter-less constructors. The exception message will indicate what property failed to initialize if this is the case.
Sergey Alexandrovich Kryukov 28-Oct-15 2:13am    
"Catch ex As Exception
Throw"
is totally pointless, misuse of exceptions. Another misuse is local exception handling. It would be justified if "finally" was needed, but assignment of the reader to null is another, third misuse.

—SA
Philippe Mori 30-Oct-15 9:51am    
What is the missing file as indicated in the exception message. This is the most important information but yet it is not provided.

In fact, if the filename contains XmlSerializers in it name then it is a completly different issue than if the file name is the same as path.

1 solution

Assuming that the problem is with serialization assemblies as otherwise it should be trivial to know why it fails....


If we tell the debugger to stop on all exception, it will stop even if an exception in handled by the framework...

A Google search of XML serialization FileNotFoundException would give a lot of links.

https://www.google.ca/?client=safari&channel=ipad_bm#channel=ipad_bm&q=xml+serialization+filenotfoundexception[^]

First two results:

http://stackoverflow.com/questions/8764742/filenotfoundexception-when-deserializing-xml[^]
http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor[^]

Thus this "problem" is well known so better to use Google that to ask same kind of question yet another time...

If you otherwise don't care for FileNotFoundException, then you can uncheck that specific option in you exception settings.

It would be nice if there would be a way to disable those exceptions in specific cases but it is either on or off switch...

NullReferenceException and TimeoutException are other exceptions that you would generally want to stop on them but not always.

Resources in ASP.NET is another case where you sometime have to handle exceptions if you want to find strings in multiple locations in a defined order as there a no function to test if a specific resource exists (well there might be some way to do it but not as easy as as TrySomething function...)

Other links will probably tell how to fix that but I never tried it. I simply ignore that in my own application as the application works as expected anyway.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 30-Oct-15 9:38am    
I voted 5, but, in a way, in advance.

The information you provided is so interesting, if not shocking, that I cannot fully believe it. The problem is: I don't think this problem has to exist. I can only suggest it's related to some real nasty bug in Microsoft code, but even then... You see, no one among the authors of the answers you quoted really understands the nature of the problem. I don't remember that I ever seriously use XmlSerializer, because I always thought it's totally wrong by the whole idea. I often recommend to use Data Contract, which is a very good thing, but the same problem with Data Contract serializers may appear in exact same way as with XmlSerializer. Unfortunately, there are places where XML serializer is used indirectly, such as with ASP.NET session variables... pretty frustrating...

Here is the thing: I don't see how this exception can appear, and nobody really explained that to me. I can see only one delicate moment, the issue of the uniqueness of the file name in presences of multi-processing in the OS, but even that does not explain the problem (because if the process is terminated unexpectedly, the auto-generated assembly file would not prevent creation of another file with the same assembly under a different name, and if it is complete, it could clean up after itself, and if two or more instances of the process are running, they could at least block their auto-generated assembly files, and it would be easy to detect); the only hint to this aspect is the advice to clean up the temp directory. It would be good to shed some light on this issue. The worst thing is: we don't really know how to reproduce the problem in more or less stable way.

Now, it gives me the idea that continuing to develop my own Data Contract software may make more sense than I thought before. (At this moment, it is not as comprehensive as Microsoft's: there are no namespaces and other limitation present, but XML looks better and the problem with auto-generated files does not exist in principle.) :-)

—SA
Philippe Mori 30-Oct-15 10:05am    
I didn't know it was not systematic...
Sergey Alexandrovich Kryukov 30-Oct-15 11:44am    
That's the problem...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900