Click here to Skip to main content
Rate this: bad
good
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 at 8-Dec-12 4:50am
   
Provide your code..
khoaniz at 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
0 Sergey Alexandrovich Kryukov 459
1 Nirav Prabtani 338
2 _Amy 170
3 PIEBALDconsult 160
4 Abhinav S 160
0 OriginalGriff 7,640
1 Sergey Alexandrovich Kryukov 6,836
2 Maciej Los 3,919
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2014
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