using System; using System.IO; using System.Collections.Generic; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace QiHe.CodeLib { /// /// XmlFile load and save object from and to xml file; /// public class XmlData { /// /// Loads the specified xmlfile. /// /// The xmlfile. /// public static DataType Load(string xmlfile) { DataType data; Type datatype = typeof(DataType); XmlSerializer mySerializer = new XmlSerializer(datatype); if (File.Exists(xmlfile)) { using (XmlTextReader reader = new XmlTextReader(xmlfile)) { data = (DataType)mySerializer.Deserialize(reader); } } else { //throw new FileNotFoundException(xmlfile); return default(DataType); } return data; } /// /// Loads the specified xmldata. /// /// The xmldata. /// public static DataType Load(Stream xmldata) { using (XmlTextReader reader = new XmlTextReader(xmldata)) { XmlSerializer mySerializer = new XmlSerializer(typeof(DataType)); return (DataType)mySerializer.Deserialize(reader); } } /// /// Loads the specified xmlfile. /// /// The xmlfile. /// The root. /// public static DataType Load(string xmlfile, string root) { DataType data; Type datatype = typeof(DataType); XmlRootAttribute rootattr = new XmlRootAttribute(root); XmlSerializer mySerializer = new XmlSerializer(datatype, rootattr); if (File.Exists(xmlfile)) { XmlTextReader reader = new XmlTextReader(xmlfile); data = (DataType)mySerializer.Deserialize(reader); reader.Close(); } else { //throw new FileNotFoundException(xmlfile); return default(DataType); } return data; } /// /// Saves the specified xmlfile. /// /// The xmlfile. /// The data. public static void Save(string xmlfile, DataType data) { XmlSerializer mySerializer = new XmlSerializer(data.GetType()); XmlTextWriter writer = new XmlTextWriter(xmlfile, System.Text.Encoding.UTF8); writer.Formatting = Formatting.Indented; mySerializer.Serialize(writer, data); writer.Close(); } } /// /// XmlFile load and save object from and to xml file; /// public class XmlFile { /// /// Loads the specified xmlfile. /// /// The xmlfile. /// The datatype. /// public static object Load(string xmlfile, Type datatype) { object data = null; XmlSerializer mySerializer = new XmlSerializer(datatype); if (File.Exists(xmlfile)) { XmlTextReader reader = new XmlTextReader(xmlfile); data = mySerializer.Deserialize(reader); reader.Close(); } else { //throw new FileNotFoundException(xmlfile); return null; } return data; } /// /// Saves the specified xmlfile. /// /// The xmlfile. /// The data. public static void Save(string xmlfile, object data) { XmlSerializer mySerializer = new XmlSerializer(data.GetType()); XmlTextWriter writer = new XmlTextWriter(xmlfile, System.Text.Encoding.UTF8); writer.Formatting = Formatting.Indented; mySerializer.Serialize(writer, data); writer.Close(); } /// /// Saves the specified xmlfile. /// /// The xmlfile. /// The root. /// The data. public static void Save(string xmlfile, string root, object data) { XmlRootAttribute rootattr = new XmlRootAttribute(root); XmlSerializer mySerializer = new XmlSerializer(data.GetType(), rootattr); XmlTextWriter writer = new XmlTextWriter(xmlfile, System.Text.Encoding.UTF8); writer.Formatting = Formatting.Indented; mySerializer.Serialize(writer, data); writer.Close(); } } /// /// TxtFile /// public class TxtFile { /// /// Creates the specified file. /// /// The file. public static void Create(string file) { StreamWriter sw = File.CreateText(file); sw.Close(); } /// /// Reads the specified file. /// /// The file. /// public static string Read(string file) { StreamReader sr = File.OpenText(file); string text = sr.ReadToEnd(); sr.Close(); return text; } //Encodings: "ASCII","UTF-8","Unicode","GB2312","GB18030" /// /// Reads the specified file. /// /// The file. /// The encoding. /// public static string Read(string file, string encoding) { return Read(file, Encoding.GetEncoding(encoding)); } /// /// Reads the lines. /// /// The file. /// The encoding. /// public static List ReadLines(string file, string encoding) { return ReadLines(file, Encoding.GetEncoding(encoding)); } /// /// Reads the specified file. /// /// The file. /// The encoding. /// public static string Read(string file, Encoding encoding) { StreamReader sr = new StreamReader(file, encoding); string text = sr.ReadToEnd(); sr.Close(); return text; } /// /// Reads the lines. /// /// The file. /// The encoding. /// public static List ReadLines(string file, Encoding encoding) { StreamReader reader = new StreamReader(file, encoding); List lines = new List(); string line = reader.ReadLine(); while (line != null) { lines.Add(line); line = reader.ReadLine(); } reader.Close(); return lines; } /// /// Writes the specified file. /// /// The file. /// The text. public static void Write(string file, string text) { StreamWriter sw = new StreamWriter(file, false, Encoding.UTF8); sw.Write(text); sw.Close(); } /// /// Writes the specified file. /// /// The file. /// The text. /// The encoding. public static void Write(string file, string text, Encoding encoding) { StreamWriter sw = new StreamWriter(file, false, encoding); sw.Write(text); sw.Close(); } /// /// Appends the specified file. /// /// The file. /// The text. public static void Append(string file, string text) { StreamWriter sw = File.AppendText(file); sw.WriteLine(text); sw.Close(); } /// /// Appends the specified file. /// /// The file. /// The text. /// The encoding. public static void Append(string file, string text, Encoding encoding) { StreamWriter sw = new StreamWriter(file, true, encoding); sw.Write(text); sw.Close(); } /// /// Gets the text from current position of reader to specified tag. /// tag should occupy just one line. /// if tag is null then read to the end of file. /// /// The reader. /// The tag. /// the text from current position of reader to specified tag, tag is not included. public static string GetText(TextReader reader, string tag) { if (tag == null) { return reader.ReadToEnd(); } StringBuilder text = new StringBuilder(); string line = reader.ReadLine(); while (line != null) { if (line == tag) { int n = Environment.NewLine.Length; if (text.Length > n) { text.Remove(text.Length - n, n); } return text.ToString(); } text.Append(line + Environment.NewLine); line = reader.ReadLine(); } return null; } /// /// Gets the text between two tags in file. /// each tag should occupy just one line. /// /// The file in UTF-8. /// The start tag. /// The end tag. /// the text between two tags in file, or null if startTag is not found. public static string GetText(string file, string startTag, string endTag) { StreamReader reader = File.OpenText(file); StringBuilder text = new StringBuilder(); bool found = false; string line = reader.ReadLine(); while (line != null) { if (line == endTag) { break; } if (found) { text.AppendLine(line); } if (line == startTag) { found = true; } line = reader.ReadLine(); } reader.Close(); if (found) return text.ToString(); return null; } /// /// Gets the text. /// /// The file. /// The start tag. /// The end tag. /// The line num. /// public static string GetText(string file, string startTag, string endTag, out int lineNum) { StreamReader reader = File.OpenText(file); StringBuilder text = new StringBuilder(); bool found = false; int linecounter = 0; lineNum = -1; string line = reader.ReadLine(); while (line != null) { linecounter++; if (line == endTag) { break; } if (found) { text.AppendLine(line); } if (line == startTag) { found = true; lineNum = linecounter + 1; } line = reader.ReadLine(); } reader.Close(); if (found) return text.ToString(); return null; } /// /// Counts the lines and chars. /// /// The file. /// The lines. /// The chars. public static void CountLinesAndChars(string file, out int lines, out int chars) { StreamReader reader = File.OpenText(file); lines = 0; chars = 0; string line = reader.ReadLine(); while (line != null) { lines++; chars += line.Length; line = reader.ReadLine(); } reader.Close(); } } /// /// BinFile /// public class BinFile { /// /// Reads the specified datafile. /// /// The datafile. /// public static object Read(string datafile) { if (File.Exists(datafile)) { IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(datafile, FileMode.Open, FileAccess.Read, FileShare.Read); object obj = formatter.Deserialize(stream); stream.Close(); return obj; } else { throw new FileNotFoundException(datafile); } } /// /// Writes the specified datafile. /// /// The datafile. /// The obj. public static void Write(string datafile, object obj) { IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(datafile, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, obj); stream.Close(); } } }