Click here to Skip to main content
12,348,894 members (62,177 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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:
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&lt<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 7-Dec-12 22:46pm
khoaniz657
Edited 7-Dec-12 23:11pm
v9
Comments
Krunal Rohit 8-Dec-12 4:50am
   
Provide your code..
khoaniz 8-Dec-12 4:57am
   
I have posted my code, thanks!

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100