|
I have object with members of different types like this:
public class Node
{
public int itemID = -1;
public int counter = 1;
public Node parent = null;
public List<Node> childs = new List<Node>();
public Node nodeLink = null;
}
And I have class tree
public class tree
{
public List<int> headerList = null;
public Dictionary<int, Node> mapItemNodes = new Dictionary<int, Node>();
public Dictionary<int, Node> mapItemLastNode = new Dictionary<int, Node>();
public Node root = new Node();
}
And I need your help to serialize and deserialize an object of the class tree
|
|
|
|
|
Rather than using fields, you're going to have to use properties if you want to use a default serializer - and don't forget the Serializable attribute on your class.
|
|
|
|
|
Note: I have two classes where I use XmlSerializer, and they have fields -which are correctly serialized.
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
So long as the fields are public, yes that will work. If there are fields that are private, they must be wrapped in public properties in order to work.
|
|
|
|
|
did your classes contain dictionaries? or would you please give me an example
modified 12-Nov-15 9:14am.
|
|
|
|
|
|
Your class "tree" contains Dictionaries - and that causes a problem: it won't be serializable by the XMLSerializer. That means: you havt to write your own serialization / deserialization code.
|
|
|
|
|
fyi: I have written a Tree structure very much like this, and had no problem serializing/de-serializing Dictionaries using WCF.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
You can use WCF (DataContract and DataMember) to serialize structures like this; in fact, I've written a very similar tree-structure, with internal Dictionaries, and it serializes/de-serializes okay. Compressing the XML output of the WCF serializer with GZip results in file sizes often less than 50% of the umcompressed file, and often with file sizes as low as 25% of the uncompressed XML. Those file sizes are comparable to the size of the structure(s) saved with binary serialization.
I gave an example of using WCF on a recent answer to a QA question here: ^].
Do read the documentation on using WCF; and study the various fields of the DataContract and DataMember Attributes that are optional; they are very useful !
Also, note that if your Class inherits from some Generic Collection, like this:
[DataMember]
public class MyClass : List<SomeOtherClass>
{
[DataMember]
public int SomeIntProp { get; set; }
} This will not serialize, even if 'SomeOtherClass is also properly adorned with WCF Attributes. I've never found an explanation of this.
So, code the generic Collection as a Property in another Class.
Suggestion: use Properties, not Fields.
Another idea: Here you'll find an implementation of a Generic approach to using WCF ... the code was inspired by the comments and ideas of Pete O'Hanlon on that thread, but if the code doesn't work for you, that's my fault
[^]
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 12-Nov-15 17:15pm.
|
|
|
|
|
Dear all,
thanks a lot for your replies, but actually I am confused because I am a new user to c#, and now I am using ConsoleApplication in my application. my aim from asking you is to write the content of the tree to a file and retreiving it a gain to the same object regardless the used method. I hope that you help me in my problem.
|
|
|
|
|
Hi,
I have a little method which registeres an extension (edit: like .ext) into the Windows registry. Does anyone have an idea how I can tell Windows to open a file with the respective extension only in a new program instance, if currently none is running? If the program is already running, a new tab in my program should be created (I have a event void opentab(object sender, EventArgs e) to open a new tab). But how can I tell windows not to open a new instance but to call the running program and let it run the event?
I think one can somehow do it with a subnode "ddeexec" in the subnode "open", but I'm not sure and it didn't work for me.
Best wishes
robin
void DefineAssociations(string extension, string IconFile)
{
string keyName = extension;
RegistryKey key = Registry.ClassesRoot.OpenSubKey(keyName);
if (key != null)
{
string apppath = "";
apppath = Application.ExecutablePath.ToLower() + " \"%1\"";
key.SetValue("", "MyProg");
RegistryKey iconkey;
iconkey = key.CreateSubKey("DefaultIcon");
iconkey.SetValue("", Environment.CurrentDirectory + "\\resources\\" + IconFile);
key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", apppath);
}
}
|
|
|
|
|
I don't think it's possible. Anyway, long story short - that's not the way to do it.
It's your job to find out if another instance is running, and if so, to notify it. It's not an easy task though. There's quite a bit of code required to do this. Namely, first find out if another instance of your app is running, and if so, you need to notify it and send it the new file to open.
More or less, something like this:
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT {
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)] public string lpData;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
public const int WM_COPYDATA = 0x004A;
...
var running_already = Process.GetProcessesByName("Yourapp").ToDictionary(x => x.Id, x => x);
running_already.Remove(Process.GetCurrentProcess().Id);
if (running_already.Count > 0) {
if (args.Length == 0)
return;
string open = args[0];
var cds = new win32.COPYDATASTRUCT {
dwData = new IntPtr(0),
cbData = open.Length * 2 + 1,
lpData = open
};
var handle = running_already.First().Value.MainWindowHandle;
win32.SendMessage(handle, win32.WM_COPYDATA, IntPtr.Zero, ref cds);
return;
}
protected override void WndProc(ref Message m) {
if (m.Msg == win32.WM_COPYDATA) {
var st = (win32.COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof (win32.COPYDATASTRUCT));
string open = st.lpData;
util.postpone(() => on_file_drop(open), 100);
}
base.WndProc(ref m);
}
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
Wow, it works up to the point at which I just need to call the file open event. Great! thanks a lot;)
robin
|
|
|
|
|
Glad it works I copied the code from my app - it took me a while to do it right
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
It's impossible; here's the code to do the impossible -
+5
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
That step is to be cared of by your program opening the file, not by the registry. The concept is called "single instance application".
|
|
|
|
|
I want to develop Windows App for NGO similar to below URL https://play.google.com/store/apps/details?id=com.savethelife.csr
This is for Non profitable Organisation, so cost should be lower side.Please suggest free database websites those can provide free data services? Thank
|
|
|
|
|
Maybe you should develop the app first before concerning yourself with "hosting".
|
|
|
|
|
That's an Android app. Are you sure this is a C# issue?
|
|
|
|
|
COder876 wrote: I want to develop Windows App for NGO Sounds like a good idea. Please come back here with specific technical questions related to your progress in programming your application using C#.
And, be sure and visit other CP forums, like the Database forum, to ask questions about the other issues with the application.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
My code is failing on the following line.
this.buCursor.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buCursor.BackgroundImage")));
Because appears that
resources.GetObject("buCursor.BackgroundImage")
Is returning NULL or some value that is causing the application to throw a
"'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
Additional information: MissingManifestResourceException"
Putting a watch on buCursor I see that the BackgroundImage element value says "Could not evaluate expression"
A bit more background is the code runs no problem on a Windows CE5.0 application with C# code developed in VS 2005.
I created a new SDK for a new target we are going to use, for Windows Embedded Compact 13 using VS2013 and application builder. This runs basic test C# applications, developed in C#, I have built for it.
I created new device application C# project in VS 2013, based on the SDK I made, and added the source code and resources from the VS2005 project.
After building the VS2013 code and debugging it, it throws this exception.
I used "Run Custom Tool" on resources.resx.
I also ensured .resx files and other resources were "Embedded Resources"
Any help would be greatly appreciated.
Thank you.
|
|
|
|
|
|
It could also be that you need to copy the files themselves (images) from the source solution, and put them in the exact same folder (the resource manager holds a reference to the file added to the .resx).
I never finish anyth
|
|
|
|
|
hi
i have DataSet that contain Image.
i need to save this Image to File.
i try this:
SQL = ItemCode,PIC from ROW;
dsView = new DataSet();
adp = new SqlCeDataAdapter(SQL, Conn);
adp.Fill(dsView, "ROW");
adp.Dispose();
foreach (DataRow R in dsROW.Tables[0].Rows)
{
ItemCode = R["ItemCode"].ToString().Trim() ;
TEMP = R["PIC"].ToString().Trim();
Image image = R["PIC"] as Image;
if(image != null)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imagedata = ms.ToArray();
}
but image Always is null
and in TEMP i see System.Byte[]
need some help, thanks
|
|
|
|