using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication1
{
class Program
{
public static void SerializeData()
{
string str = "Welcome at TutorialRide.";
// Create file to save the data.
FileStream fs = new FileStream(@"D:\MyDataFile.txt", FileMode.Create);
// BinaryFormatter object will perform the serialization
BinaryFormatter bf = new BinaryFormatter();
// Serialize() method serializes the data to the file
bf.Serialize(fs, str);
// Close the file
fs.Close();
}
public static void DeSerializeData()
{
// Open file to read the data
FileStream fs = new FileStream(@"D:\MyDataFile.txt", FileMode.Open);
// BinaryFormatter object performs the deserialization
BinaryFormatter bf = new BinaryFormatter();
// Create the object to store the deserialized data
string data = "";
data = (string)bf.Deserialize(fs);
//// Close the file
fs.Close();
// Display the deserialized strings
Console.WriteLine("Your deserialize data is ");
Console.WriteLine(data);
}
static void Main(string[] args)
{
SerializeData();
DeSerializeData();
Console.ReadLine();
}
}
}
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication1
{
[Serializable]
class Employee
{
public int EmpID;
public string EmpName;
public string Address;
public Employee(int id,string name, string EmpAdd)
{
EmpID = id;
EmpName = name;
Address = EmpAdd;
}
}
class Program
{
public static void SerializeData()
{
Employee obj = new Employee(1,"Raj","Pune");
// Create file to save the data.
FileStream fs = new FileStream(@"D:\Employee.txt", FileMode.Create);
// BinaryFormatter object will perform the serialization
BinaryFormatter bf = new BinaryFormatter();
// Serialize() method serializes the data to the file
bf.Serialize(fs, obj);
// Close the file
fs.Close();
}
public static void DeSerializeData()
{
Employee emp;
// Open file to read the data
FileStream fs = new FileStream(@"D:\Employee.txt", FileMode.Open);
// BinaryFormatter object performs the deserialization
BinaryFormatter bf = new BinaryFormatter();
// Create the object to store the deserialized data
emp = (Employee)bf.Deserialize(fs);
int id = emp.EmpID;
string name = emp.EmpName;
string Empadd = emp.Address;
//// Close the file
fs.Close();
// Display the deserialized strings
Console.WriteLine("Your deserialize data is ");
Console.WriteLine("EmpID = "+id);
Console.WriteLine("Emp Name = " + name);
Console.WriteLine("Emp Address = " + Empadd);
}
static void Main(string[] args)
{
SerializeData();
DeSerializeData();
Console.ReadLine();
}
}
}
using System;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
public static void SerializeData()
{
// Create file to save the data.
FileStream fs = new FileStream(@"D:\MyXMLFile.txt", FileMode.Create);
//XmlSerializer object will perform the serialization
XmlSerializer xmlObj = new XmlSerializer(typeof(DateTime));
// Serialize mehtod serialize the data to the file
xmlObj.Serialize(fs, System.DateTime.Now);
// Close the file
fs.Close();
}
public static void DeSerializeData()
{
// Open file to read the data
FileStream fs = new FileStream(@"D:\MyXMLFile.txt", FileMode.Open);
// XmlSerializer object to perform the deserialization
XmlSerializer xs = new XmlSerializer(typeof(DateTime));
// Use the XmlSerializer object to deserialize the data from the file
DateTime time = (DateTime)xs.Deserialize(fs);
// Close the file
fs.Close();
// Display the deserialized time
Console.WriteLine("Day: " + time.DayOfWeek + ", Time: " + time.TimeOfDay.ToString()+", Date: "+time.Date.ToShortDateString());
}
static void Main(string[] args)
{
SerializeData();
DeSerializeData();
Console.ReadLine();
}
}
}
using System;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
public class Employee
{
public int EmpID{get;set;}
public string EmpName { get; set; }
public string Address { get; set; }
public Employee()
{
EmpID = 0;
EmpName = "N/A";
Address = "N/A";
}
}
class Program
{
public static void SerializeData(Employee emp)
{
// Create file to save the data.
FileStream fs = new FileStream(@"D:\MyXMLFile.txt", FileMode.Create);
//XmlSerializer object will perform the serialization
XmlSerializer xmlObj = new XmlSerializer(typeof(Employee));
// Serialize mehtod serialize the data to the file
xmlObj.Serialize(fs, emp);
// Close the file
fs.Close();
}
public static void DeSerializeData()
{
// Open file to read the data
FileStream fs = new FileStream(@"D:\MyXMLFile.txt", FileMode.Open);
// XmlSerializer object to perform the deserialization
XmlSerializer xs = new XmlSerializer(typeof(Employee));
// Use the XmlSerializer object to deserialize the data from the file
Employee emp = (Employee)xs.Deserialize(fs);
// Close the file
fs.Close();
// Display the de-serialized data
Console.WriteLine("Employee ID = "+emp.EmpID);
Console.WriteLine("Employee Name = " + emp.EmpName);
Console.WriteLine("Employee Address = " + emp.Address);
}
static void Main(string[] args)
{
Employee empObj = new Employee();
Console.WriteLine("Enter employee ID");
empObj.EmpID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter employee Name");
empObj.EmpName = Console.ReadLine();
Console.WriteLine("Enter employee address");
empObj.Address = Console.ReadLine();
SerializeData(empObj);
DeSerializeData();
Console.ReadLine();
}
}
}
[XmlRoot("EmployeeDetail")]
public class Employee
{
public int EmpID{get;set;}
public string EmpName { get; set; }
[XmlAttribute("EmployeeType")]
public string EmpType { get; set; }
[XmlElement("EmpAddress")]
public string Address { get; set; }
[XmlIgnore]
public double Salary { get; set; }
public Employee()
{
EmpID = 0;
EmpName = "N/A";
Address = "N/A";
}
}
<?xml version="1.0"?>
<EmployeeDetail EmployeeType="Permanent">
<EmpID>1</EmpID>
<EmpName>Raj</EmpName>
<EmpAddress>Pune</Address>
</EmployeeDetail>
EmpID | EmpName | Address |
---|---|---|
1 | Raj | Pune |
2 | Swaraj | USA |
using System;
using System.Data;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
public static void SerializeData()
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet));
// Create a DataSet;
DataSet empDS = new DataSet("emp");
// Create DataTable.
DataTable empDT = new DataTable("Employee");
// Specify columns of a DataTable
empDT.Columns.Add("EmpID"); //Column1
empDT.Columns.Add("EmpName"); //Column2
empDT.Columns.Add("Address"); //Column3
// Add rows in DataTable
empDT.Rows.Add("1", "Raj", "Pune");
empDT.Rows.Add("2", "Swaraj", "USA");
// Add DataTable in DataSet
empDS.Tables.Add(empDT);
// Create an object of stream writer.
FileStream fs = new FileStream(@"D:\DataSetFile.xml", FileMode.Create);
// Serialize the instance of BasicSerialization
xmlSerializer.Serialize(fs, empDS);
// Close the stream writer
fs.Close();
}
static void Main(string[] args)
{
SerializeData();
Console.ReadLine();
}
}
}
<?xml version="1.0"?>
<DataSet>
<xs:schema id="emp" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="emp" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="EmpID" type="xs:string" minOccurs="0" />
<xs:element name="EmpName" type="xs:string" minOccurs="0" />
<xs:element name="Address" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<emp>
<Employee diffgr:id="Employee1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<EmpID>1</EmpID>
<EmpName>Raj</EmpName>
<Address>Pune</Address>
</Employee>
<Employee diffgr:id="Employee2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
<EmpID>2</EmpID>
<EmpName>Swaraj</EmpName>
<Address>USA</Address>
</Employee>
</emp>
</diffgr:diffgram>
</DataSet>