Serialization file not found

This is the code, that once worked, but since I changed my computer it doesn't anymore.
FileStream RMSWriter = null;
try
{
XmlSerializer mySerializer = new XmlSerializer(typeof(RMSSettings));
RMSWriter = new FileStream(Constants.Application_Path + Constants.RMS_threshold_file, FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
mySerializer.Serialize(RMSWriter, Settings.Instance.RmsSettings);
}
catch (Exception ex)
{
throw ex;
}
finally
{
RMSWriter.Close();
}
Th error that it gives, is that it doesn't find the file specified, although the path is correct and is absolute.
I tried using a StreamWriter created with File.CreateText, also tried creating the filestream with File.Create, but in both cases I have the error that the file is in use by another process. This error I get also when I don't specify the FileAccess and FileSharing parameters.

Any idea why it doesn't work anymore? I have .Net Framework 2.0 installed and also the compact framework 2.0 and 1.0 SP3 Developer.
Also the error is reproductible on other development computers.
I have full rights on the folder where I want to save the file, and also in security policies for the framework I had all code for the users, and all permisions set to the code coming from local machine.
LVL 1
marioanaAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
I still suspect is a problem inside your class, some member that cannot be serialized.
Please try with a class like:

public class TestClass
{
       public string testStr = "test string";
}

TestClass tc = new TestClass();
XmlSerializer mySerializer = new XmlSerializer(typeof(TestClass));
mySerializer.Serialize(RMSWriter, tc);

0
 
Jaime OlivaresSoftware ArchitectCommented:
try to test the filename:

XmlSerializer mySerializer = new XmlSerializer(typeof(RMSSettings));
string fn = Constants.Application_Path + Constants.RMS_threshold_file;
System.Windows.Forms.MessageBox.Show(fn);
RMSWriter = new FileStream(fn, FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
mySerializer.Serialize(RMSWriter, Settings.Instance.RmsSettings);
0
 
marioanaAuthor Commented:
The path is valid, I saw it with the debugger. Also I tried saving the file directly to D drive and it doesn't work either.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
Better you try with a XmlWriter object:

XmlWriter RMSWriter= null;
XmlSerializer mySerializer = new XmlSerializer(typeof(RMSSettings));
string fn = Constants.Application_Path + Constants.RMS_threshold_file;
RMSWriter= XmlTextWriter.Create(fn);
mySerializer.Serialize(RMSWriter, Settings.Instance.RmsSettings);
0
 
marioanaAuthor Commented:
I tryied with a XmlWriter but I get this error:
"The process cannot access the file 'C:\\RMSThresholds.xml' because it is being used by another process."}      System.SystemException {System.IO.IOException}
This exception I get also with the FileStream if I don't set the FileAccess and FileSharing.
0
 
Jaime OlivaresSoftware ArchitectCommented:
OK, try with the following code, be sure to close the reader every time.
string fn = Constants.Application_Path + Constants.RMS_threshold_file;
FileStream stream = new FileStream(fn, FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
XmlSerializer mySerializer = new XmlSerializer(typeof(RMSSettings));
XmlWriter RMSWriter = XmlTextWriter.Create(stream);
mySerializer.Serialize(RMSWriter, Settings.Instance.RmsSettings);
RMSWriter.Close();
stream.Close();

Open in new window

0
 
marioanaAuthor Commented:
Thse same :
-base      {"There was an error generating the XML document."}      System.Exception {System.InvalidOperationException}
+InnerException      {"The system cannot find the file specified. (Exception from HRESULT: 0x80070002)":null}      System.Exception {System.IO.FileNotFoundException}

0
 
Jaime OlivaresSoftware ArchitectCommented:
Well, let's try step by step, starting with:
string fn = Constants.Application_Path + Constants.RMS_threshold_file;
FileStream stream = new FileStream(fn, FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);

test if stream is null or not, if null, then you still have a problem either with filename or file access rights.

0
 
marioanaAuthor Commented:
The stream is not null and his name is the path. And as I said, even if I set the path to "D:\\Thresholds.xml" I still have this error, and on D drive I have full control. That is why is strange it gives this error.
0
 
Jaime OlivaresSoftware ArchitectCommented:
if stream is not null in my example, which line produces the exception?
0
 
marioanaAuthor Commented:
This is the line: mySerializer.Serialize(RMSWriter, Settings.Instance.RmsSettings);
0
 
Jaime OlivaresSoftware ArchitectCommented:
it should work, I think it is not related with the code
>>"The process cannot access the file 'C:\\RMSThresholds.xml' because it is being used by another process."}
maybe you are reading the file with some text editor? or you have opened that file inside your IDE?
0
 
marioanaAuthor Commented:
no, it's not opened :(. I have a program which tells me if the file is locked by a process..the file is not locked until I create the filestream, so the only lock is coming from my filestream which is normal. So no lock, and when I try to create the filestream the file is locked, and the filestream gives the error: >>"The process cannot access the file 'C:\\RMSThresholds.xml' because it is being used by another process."} .
If the file does not exist it creates it then it gives the error, and the file remains created with a size of 0Kb.
0
 
Jaime OlivaresSoftware ArchitectCommented:
I would try to serialize a simpler object, like a string, something like:
XmlSerializer mySerializer = new XmlSerializer(typeof(string));
mySerializer.Serialize(RMSWriter, "just testing");

0
 
marioanaAuthor Commented:
I tried both serializing a string and an int. The error was
{"Token StartElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment. "}
0
 
marioanaAuthor Commented:
what i don't understand is why is not working now. A few months ago when I tested it it worked perfectly. Since then I added another project to the solution and worked on that one, so there was no modification to this one :(.
0
 
marioanaAuthor Commented:
I created this class and it gives the same error. Although when I tried the second time to serialize a double, int or string it worked. I don't understand, I also set the serializable atribute to all clases involved...
[Serializable]
    public class TestSerialize
    {
        private string test1 = "test1";
 
        public string Test1
        {
            get { return test1; }
            set { test1 = value; }
        }
        private int test2 = 10;
 
        public int Test2
        {
            get { return test2; }
            set { test2 = value; }
        }
    }

Open in new window

0
 
marioanaAuthor Commented:
It seems is the contex :(. I made a unit test creating the RMSSettings and populating it with the defaults, and it worked. It seems that the main application (my application is attached to the main application through COM) it blocks somehow the serialization of clases. But no idea  how to see what is going on...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.