Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have a class employees.

I have one generic class <tkey,tdata>

I have one dictionary <tkey,tdata>

I want to store the Employee info to a file.

After saving the object of Employees to Tdata (value) of dictionary, I cannot get the info of employee (firstname, lastname...) from dictionary in order to create a xml file.

Any idea to cast the value of dictionary to Employee to get the properties of employee class?

I can get the object of Employee by a foreach(var item in dictionary) => item.Value
but I cannot cast to get the properties of the employee class!

Thank you!

======code

Main Class:
C#
EmployeeMNG<string,employees> empMNG= new EmployeeMNG<string,employees>();
 

Employees s= new Employees();
s.empNo = 11;
s.firstName = "khoaxml";
s.lastName = "nguyen";
s.address = "47 nguyen hong";
s.country = "VN";
 
empMNG.add("stu", s); ///add to dictionary
empMNG.save("file.xml");
 
Generic Class:
 
public class EmpMNG<tkey,tdata> : IEmpMNG<<tkey,tdata>
{
private Dictionary<tkey,tdata> cache;
 
//Constructor
public EmpMNG()
{
cache = new Dictionary<tkey,tdata>();
}
 
//add to dictionary
public TData add(TKey key, TData data)
{
if (!cache.ContainsKey(key))
{
cache[key] = data;
}
return cache[key];
}
public void Save(string xmlPath)
{
foreach (var item in cache)//cache is dictionary
{
Console.WriteLine(item.Value);//here I get object of Employee class, which is passed from the main class
//is there any way to get the properties of Employee from here? I dont know how to cast with the Dictionary, thank you!!
 
}
}
Posted
Updated 7-Dec-12 23:11pm
v9
Comments
[no name] 8-Dec-12 4:50am    
Provide your code..
khoaniz 8-Dec-12 4:57am    
I have posted my code, thanks!

1 solution

Use my article for serializing the data : fastJSON[^]
It is better and more forgiving than XML serialization.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900