|
I want to serialize an array of objects, using the SOAP formatter. However, when serializing the second object, I get an exception that says I cannot add the same object twice to a serialization info. I wrote a small application to demonstrate the problem:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serialization
{
[Serializable]
public class MyObject:ISerializable
{
public Int32 idx=0;
public MyObject(Int32 i)
{
idx=i;
}
public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
{
info.AddValue("Idx",idx);
}
}
[Serializable]
class ObjectList: ISerializable
{
public List<MyObject> TheList;
public ObjectList()
{
TheList=new List<MyObject>(10);
MyObject tmp=new MyObject(1);
TheList.Add(tmp);
tmp=new MyObject(2);
TheList.Add(tmp);
}
public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
{
Int32 i=0;
for(i=0;i<2;i++)
{
if(TheList[i]!=null)
{
TheList[i].GetObjectData(info,context);
}
}
}
}
class Program
{
static void Main(string[] args)
{
ObjectList MyObjectList=new ObjectList();
FileStream WriteStream=new FileStream("test.xml",FileMode.Create);
SoapFormatter bfor=new SoapFormatter();
bfor.Serialize(WriteStream,MyObjectList);
}
}
}
In the sample I create a class MyObject. Objects of type MyObject are stored in an ObjectList object, which essentially is a wrapper around a LIST<myobject> generic List object.
I have no idea what I'm doing wrong. I did not find any samples on how to handle array serialization. Please note that the application I develop is far more complex than this. Therefore I do not want to use automatic serialization. I use the SOAP formatter because it is easier to debug. Later I will switch to the binary formatter.
Tanks in advance for your advice.
Rudolf Heijink
|
|
|
|
|
Rudolf Jan Heijink wrote: ObjectList MyObjectList=new ObjectList();
try this:
List<MyObject> list = new List<MyObject>();<br />
bfor.Serialize(WriteStream, list);<br />
|
|
|
|
|
Thank you very much for your suggestion.
Unfortunately, your suggestion does not work with the SOAP formmatter. The SOAP formatter seems not to support generic types. With the Binary formatter it seems to work, at least no exceptions or error messages.
Still, I do not like it. I still think it is quite normal to serialize a number of objects. Another thing is that you should not invoke the Soap formatter more than once, because it generates XML headers. (i tried to do this)
I also think there is a difference betwee a class containing al List and maybe other objects (as I will need in my application) and a list. So, I'm not happy yet. Thanks anway.
Rudolf Heijink
|
|
|
|
|
I did some additional tests. It seems the problem has nothing to do with lists, but possibly there is a bug in .NET. If I try to serialize two objects of the same type, I get the error message that I cannot serialize the same object twice. It mey be an error in the translation of the .NET error messages. I cannot check that. I have a Dutch Windows version trepresenting all .NET errors in Dutch.
Below the code to demonstrate the problem:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serialization
{
[Serializable]
public class MyObject:ISerializable
{
public Int32 idx=0;
public MyObject(Int32 i)
{
idx=i;
}
internal MyObject(SerializationInfo info,StreamingContext context)
{
idx=info.GetInt32("Idx");
}
public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
{
info.AddValue("Idx",idx);
}
}
[Serializable]
class ObjectList: ISerializable
{
MyObject object1;
MyObject object2;
public ObjectList()
{
object1=new MyObject(1);
object2=new MyObject(2);
}
protected ObjectList(SerializationInfo info,StreamingContext context)
{
object1=new MyObject(info,context);
object2=new MyObject(info,context);
}
public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
{
object1.GetObjectData(info,context);
object2.GetObjectData(info,context);
}
}
class Program
{
static void Main(string[] args)
{
ObjectList MyObjectList=new ObjectList();
FileStream WriteStream=new FileStream("test.xml",FileMode.Create);
SoapFormatter bfor=new SoapFormatter();
MyObject Object1=new MyObject(1);
MyObject object2=new MyObject(2);
bfor.Serialize(WriteStream,MyObjectList);
}
}
}
Rudolf Heijink
|
|
|
|
|
|
Sad story,
I think I understand much more now why things went wrong in my application. I will write an article about it for codeproject, so coming soon.
Thanks anyway for your help.
Rudolf Heijinbk
|
|
|
|
|
how do i program a form window (in c#) to come above the taskbar when ControlBox property is set to false as it slides under the taskbar.
|
|
|
|
|
Hi all,
I has just create an add-in for Visual Studio .NET 2005. I want to create an setup for that add-in so that I can share it to my friends. But I don't know how to do it. Please help me.
Thanks a lot.
imagic
|
|
|
|
|
This article[^] talks about creating an add-in for VS as well as creating a setup project for the add-in, allowing it to be installed on other machines with Visual Studio.
|
|
|
|
|
Thanks a lot.
But I have a problem at Figure 5 (Miscellaneous options for the add-in). In Visual Studio 2005, I can't find "Setting up access privileges" in Add-in Wizard (Page 4 of 6).
Please help me.
Thanks lots.
imagic
|
|
|
|
|
Oh yeah, just create a setup project and modify registry. I done . Thx you.
imagic
|
|
|
|
|
I have a set of images in a window, each loaded in a panel.
I am trying to replace them one by one with new pictures, this way: clicking on a picture in order to open a browse window from where I then choose the replacement.
My problem is that I can't finalize the operation as I get the error "The process cannot access the file '...' because it is being used by another process." This is because I am trying to change the source that started the click event!
How can I play a trick on in to make it work?
P.S. I should also mention that in the click event I am using the File.Copy(source, destination, true) method.
Good gives us the power of creating the future. Use this power with reason.
|
|
|
|
|
Hi,
maybe this is the problem: Image class keeps the file open when you somehow load the
image from file (I guess because that way they dont have to load all the metadata until
it is really needed).
The way to circumvent that is by using an intermediate image, like so (approx):
Bitmap image1=Image.FromFile(filename);
Bitmap image2=new Bitmap(image1);
image1.Dispose();
Hope this helps.
|
|
|
|
|
Hi im making an app for myself so i dont need any security , Basicly i have a mirror copy of my server on my harddisk , and a xml file that my application edits .
E:\\Remote Server\\Site Data\\Instent Alerts\\Status.xml
So what id like to do is take that file and upload it onto my server to Site Data\\Instent Alerts\\Status.xml.
Its not a public server therfore ill need to use my username and password but i cant find any examples with this online
Can anyone help me out
|
|
|
|
|
Raymond_P* wrote: i dont need any security
Why aren't you using FTP?
|
|
|
|
|
Thhats what i ment , i want to fund a way of uploading the file overwriting the old one using FTP .
But all the examples ive seen are for annonimus access i need to use my username and pass .
I wont be giving ths to anyone else so its ok to intagrate tjhe username and pass in the app
|
|
|
|
|
Raymond_P* wrote: i dont need any security
Raymond_P* wrote: i need to use my username and pass .
Raymond, I'm pretty sure a requirement to use a login and password qualifies as "needing security".
|
|
|
|
|
i ment its ok for me to put my username and pass into the application i wont be handing it out to anyone .
I just need to upload and overwrite a single file , any ideas ?
|
|
|
|
|
Raymond_P* wrote: Thhats what i ment , i want to fund a way of uploading the file overwriting the old one using FTP .
Raymond_P* wrote: i ment its ok for me to put my username and pass into the application i wont be handing it out to anyone .
In the future you might find it is more productive to put what you "mean" in your first post rather than making people drag information out of you. At this point I have lost interest in helping you.
|
|
|
|
|
Has anyone got a reliable multithreaded file copy program with progress display ? I love robocopy, but no c# wrapper is available and I can't track individual file progress.
thanks!
h.
|
|
|
|
|
hk11 wrote: Has anyone got a reliable multithreaded file copy program with progress display ?
Well it's been three hours since you posted, do you have that finished yet?
|
|
|
|
|
hk11 wrote: I can't track individual file progress.
Yes you can. Use a FileStream to read the bytes and a FileStream to write the bytes. Read and write pieces at a time to do your own progress of copying a file.
|
|
|
|
|
Hi I want to create a Listbox which Displays Text but instead of showing the white background I want the Background to be that of its Parent( its containing Form or Panel). So We have a Listbox displaying its contents as text but the background is the image which I've set for its Form.
Thanks...
|
|
|
|
|
listBox1.BackColor = this.BackColor;
Hope this will work.
Thanks,
Anand.
|
|
|
|
|
I have an Image in the Background (on the containing Form). So Setting the List's color to a single Color wont help.
Thanks for Replying. I Appreciate it...
|
|
|
|