Solved

Serialization file not found

Posted on 2007-11-14
18
742 Views
Last Modified: 2013-11-07
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.
0
Comment
Question by:marioana
  • 10
  • 8
18 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20279943
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
 
LVL 1

Author Comment

by:marioana
ID: 20280077
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20280249
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
 
LVL 1

Author Comment

by:marioana
ID: 20287263
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20287278
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
 
LVL 1

Author Comment

by:marioana
ID: 20287345
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20287347
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
 
LVL 1

Author Comment

by:marioana
ID: 20287450
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20287474
if stream is not null in my example, which line produces the exception?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:marioana
ID: 20287492
This is the line: mySerializer.Serialize(RMSWriter, Settings.Instance.RmsSettings);
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20287527
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
 
LVL 1

Author Comment

by:marioana
ID: 20287582
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20287631
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
 
LVL 1

Author Comment

by:marioana
ID: 20287667
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
 
LVL 1

Author Comment

by:marioana
ID: 20287670
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20287723
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
 
LVL 1

Author Comment

by:marioana
ID: 20287800
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
 
LVL 1

Author Comment

by:marioana
ID: 20287905
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now