Monday, February 4, 2013
XML and Binary File Read Write
public static bool WriteXML<T>(string fileName, IEnumerable<T> objList)
{
DataContractSerializer x = new DataContractSerializer(new ObservableCollection<T>().GetType());
FileStream writer = new FileStream(fileName, FileMode.Create);
x.WriteObject(writer, objList);
writer.Flush();
writer.Close();
writer.Dispose();
return true;
}
public static void ReadXML(string fileName)
{
/*here object means your business object*/
FileStream fs = new FileStream(fileName, FileMode.Open);
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, null);
DataContractSerializer ser = new DataContractSerializer(typeof(Object));
// Deserialize the data and read it from the instance.
Object deserialized = (Object)ser.ReadObject(reader, true);
reader.Close();
fs.Close();
}
public bool CreateXML<T>(List<T> objList, string fileName)
{
string path = fileName + ".xml";
XmlSerializer x = new XmlSerializer(new List<T>().GetType());
StreamWriter writer = new StreamWriter(path);
x.Serialize(writer, objList);
writer.Flush();
writer.Close();
writer.Dispose();
return true;
}
public void ReadXML(string fileName)
{
DataSet ds = new DataSet();
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
ds.ReadXml(fs);
fs.Close();
fs.Dispose();
}
public bool CreateBinaryDataFile<T>(List<T> objList, string xmlFileName)
{
using (FileStream str = File.Create(xmlFileName))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(str, objList);
str.Flush();
str.Close();
str.Dispose();
}
return true;
}
public bool ReadBinaryDataFile(string xmlFileName)
{
string myXMLfile = @"D:\" + xmlFileName;
List<object> _IssueList = new List<object>();
using (FileStream str = File.OpenRead(myXMLfile))
{
BinaryFormatter bf = new BinaryFormatter();
_IssueList = (List<object>)bf.Deserialize(str);
str.Flush();
str.Close();
str.Dispose();
return true;
}
}
Labels:
Silverlight
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment