Wodzu
Wodzu

Reputation: 6989

XmlSerializer.Serialize doesn't save a file and doesn't throw an exception

I am having problem with saving of my object. Take a look at this code:

public void SerializeToXML(String FileName)
{
    XmlSerializer fSerializer = new XmlSerializer(typeof(Configuration));
    using (Stream fStream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        fSerializer.Serialize(fStream, this);
    }
}

The problem is that when the user does not have rights to the location on hard disk, this function will not throw me any exception and do not save my file. For example saving to "C:\test.xml" act like nothing happened. And I would like to know if the file has not been saved and it would be good to know the reason why.

I know that I could check if the file on given location exists and throw an exception manualy but shouldn't this be done by the XmlSerializer or FileStream itself?

Thanks for your time

Edit:

As I was suspecting I had to turn on some additional debugging. Since I am using the using clause, the "Enable unmanaged code debugging option" must be check in project properties under the Debug section. After this, the exception is shown in the debugging process.

Edit2

Replacing the above using clause with this code triggers the exception:

public void SerializeToXML(String FileName)
{
    XmlSerializer fSerializer = new XmlSerializer(typeof(Configuration));
    Stream fStream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None);
    try
    {
        fSerializer.Serialize(fStream, this);
    }
    finally
    {
        fStream.Close();
    }
}

Upvotes: 1

Views: 1958

Answers (3)

Hans Passant
Hans Passant

Reputation: 942257

Vista file redirection can explain this. It is a feature to allow legacy programs that don't handle UAC properly to still operate. The file gets redirected to the virtual store.

This will happen when you use Visual Studio 2005 or earlier or did something to prevent the manifest from getting embedded in the exe. Fix it by including a manifest.

Upvotes: 1

Daniel A.A. Pelsmaeker
Daniel A.A. Pelsmaeker

Reputation: 50356

I suspect that it has to do with your using statement, because it uses a hidden try-finally construction (where the Dispose() method is called in the finally clause). Information about exceptions which are not visible outside the using block can be found here.

Try replacing the using statement by an instantiation and a call to it's Dispose() method, and I think your problem is solved. Of course, you should afterwards enclose your code in an explicit try-finally structure as part of good programming practice.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503220

That sounds very strange to me - and it doesn't sound like it has anything to do with the serializer. If you don't have access rights to a particular location, then creating the FileStream should throw an exception; it shouldn't wait until the Serialize line.

Are you absolutely sure that you don't have some catch block higher up which is hiding the problem from you?

Upvotes: 1

Related Questions