.....
My requirement is i am taking one class like
class employee
{
int id;
string name;
float sal;
}
class company
{
list
public void add(...)
public void show(...)
}
main()
{
add(..)-->for adding the elements into xml file
show(...)-->is used to show the xml data
}
can u help the coding?
VulpesPosted Aug 19, 2011, 8:43 AM
VulpesPosted Aug 19, 2011, 12:04 PM
Here's the code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace DakeTest
{
public class Employee
{
public string ID {get; set;}
public string Name {get; set;}
public decimal Salary {get; set;}
}
public class Company: IComparable
{
public string Name {get; set;}
public List
public Company()
{
}
public Company(string aName)
{
Name = aName;
}
public override string ToString()
{
return string.Format("Company {0}: Employees {1}", Name, Employees.Count);
}
public int CompareTo(Company other)
{
// If other.Name is null, this instance is greater.
if (other.Name == null) return 1;
// Otherwise, compare the Names of this and the other instance
return this.Name.CompareTo(other.Name);
}
}
class Program
{
static XmlSerializer xs;
static void Main(string[] args)
{
XmlRootAttribute root = new XmlRootAttribute();
root.ElementName = "Companies";
root.Namespace = "";
root.IsNullable = false;
xs = new XmlSerializer(typeof(List
List
ShowCompanies(companies);
// now sort the Companies by Name and redisplay
companies.Sort();
ShowCompanies(companies);
Save("companies.xml", companies);
companies = Load
>("companies.xml");
ShowCompanies(companies);
Save
>("companies.xml", companies);
Load(string FileName) companies;
)xs.Deserialize(s);
companies)
(string FileName)
(string FileName, T t)
companies)
}
private static List
{
List
using(Stream s = File.OpenRead(FileName))
{
companies = (List
}
return companies;
}
private static void Save(string FileName, List
{
using(Stream s = File.OpenWrite(FileName))
{
xs.Serialize(s, companies);
}
}
private static T Load
{
T objects;
using(Stream s = File.OpenRead(FileName))
{
objects = (T)xs.Deserialize(s);
}
return objects;
}
private static void Save
{
using(Stream s = File.OpenWrite(FileName))
{
xs.Serialize(s, t);
}
}
private static void ShowCompanies(List
{
Console.WriteLine("List of companies:\n\n");
foreach (Company company in companies)
{
Console.WriteLine(company);
}
Console.WriteLine("Done:\n\n\n");
}
}
}
satish babuPosted Aug 19, 2011, 9:37 AM
>("companies.xml"); ShowCompanies(companies); Save
>("companies.xml", companies); } //TASK 1 - complete this private static List Load(string FileName)
{
//TODO: complete this
return null;
}
//TASK 1 - complete this
private static void Save(string FileName, List companies)
{
//TODO: complete this
}
//TASK 3 - advanced - complete this
private static T Load(string FileName)
{
//TODO: complete this
return default(T);
}
//TASK 3 - advanced - complete this
private static void Save(string FileName, T t)
{
//TODO: complete this
}
private static void ShowCompanies(List companies)
{
System.Console.WriteLine("List of companies:\n\n");
foreach (Company company in companies)
{
System.Console.WriteLine(company.ToString());
}
System.Console.WriteLine("Done:\n\n\n");
}
}
}
satish babuPosted Aug 19, 2011, 9:22 AM