private void OpenFileCmd(string fileName) { if (String.IsNullOrEmpty(fileName) || !File.Exists(fileName)) { return; } curDoc = AppBase.LoadFile(fileName); if (curDoc == null) { MessageBox.Show("Failed to load seleted file, check file format."); } else { rTBDoc.Clear(); rTBDoc.Text = curDoc.content; if (!String.IsNullOrEmpty(curDoc.intent)) { cbIntent.Text = curDoc.intent; } if (!String.IsNullOrEmpty(curDoc.type)) { cBType.Text = curDoc.type; } lbMessage.Text = String.Format("Message:\n Tilte:{0}\n Author:{1}\n URL:{2}\n ID:{3}", curDoc.title, curDoc.author, curDoc.url, curDoc.id); RefreshRichTextBox(-1, -1); curDocFileName = Path.GetFileNameWithoutExtension(fileName); if (curDocFileName.EndsWith(".xml")) { curDocFileName = curDocFileName.Substring(0, curDocFileName.Length - 4); } } }
private void tSbtOpen_Click(object sender, EventArgs e) { using (OpenFileDialog ofDlg = new OpenFileDialog()) { ofDlg.DefaultExt = ".xml"; ofDlg.Filter = "XML File|*.xml|Work State File|*.ws|All File|*.*"; ofDlg.Multiselect = false; if (ofDlg.ShowDialog() == DialogResult.OK) { curDoc = AppBase.LoadFile(ofDlg.FileName); if (curDoc == null) { MessageBox.Show("Failed to load seleted file, check file format."); } else { rTBDoc.Clear(); rTBDoc.Text = curDoc.content; if (!String.IsNullOrEmpty(curDoc.intent)) { cbIntent.Text = curDoc.intent; } if (!String.IsNullOrEmpty(curDoc.type)) { cBType.Text = curDoc.type; } lbMessage.Text = String.Format("Message:\n Tilte:{0}\n Author:{1}\n URL:{2}\n ID:{3}", curDoc.title, curDoc.author, curDoc.url, curDoc.id); RefreshRichTextBox(-1, -1); curDocFileName = Path.GetFileNameWithoutExtension(ofDlg.FileName); if (curDocFileName.EndsWith(".xml")) { curDocFileName = curDocFileName.Substring(0, curDocFileName.Length - 4); } } } } }
public static bool SaveFile(string fileName, TagDoc savedDoc) { if (savedDoc == null || String.IsNullOrEmpty(savedDoc.type) || String.IsNullOrEmpty(savedDoc.id) || string.IsNullOrEmpty(savedDoc.content)) { return(false); } string objFileName = fileName + ".ws"; HistoryNode curHistoryNode = savedDoc.historyList[savedDoc.historyList.Count - 1]; curHistoryNode.saveDateTime = DateTime.Now; using (FileStream fs = File.Create(objFileName)) { BinaryFormatter binFormatter = new BinaryFormatter(); binFormatter.Serialize(fs, savedDoc); fs.Flush(); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><doc id=\"\"><type></type><intent></intent><content xml:space=\"preserve\"></content></doc>"); XmlNode curNode = xmlDoc.SelectSingleNode("/doc[@id]"); curNode.Attributes["id"].Value = savedDoc.id; if (!String.IsNullOrEmpty(savedDoc.title)) { XmlAttribute newAttribute = xmlDoc.CreateAttribute("title"); newAttribute.Value = savedDoc.title; curNode.Attributes.Append(newAttribute); } if (!String.IsNullOrEmpty(savedDoc.author)) { XmlAttribute newAttribute = xmlDoc.CreateAttribute("author"); newAttribute.Value = savedDoc.author; curNode.Attributes.Append(newAttribute); } if (!String.IsNullOrEmpty(savedDoc.url)) { XmlAttribute newAttribute = xmlDoc.CreateAttribute("ref"); newAttribute.Value = savedDoc.url; curNode.Attributes.Append(newAttribute); } curNode = xmlDoc.SelectSingleNode("/doc/type"); curNode.InnerText = savedDoc.type; curNode = xmlDoc.SelectSingleNode("/doc/intent"); curNode.InnerText = savedDoc.intent; curNode = xmlDoc.SelectSingleNode("/doc/content"); curNode.InnerXml = savedDoc.FormatContent(); XmlWriterSettings xwSettings = new XmlWriterSettings(); xwSettings.Indent = true; xwSettings.IndentChars = "\t"; xwSettings.Encoding = Encoding.UTF8; using (FileStream fs = File.Create(fileName)) { using (XmlWriter xw = XmlWriter.Create(fs, xwSettings)) { xmlDoc.WriteTo(xw); xw.Flush(); } fs.Flush(); } return(true); }
//static functions here public static TagDoc LoadFile(string fileName) { if (!File.Exists(fileName)) { return(null); } TagDoc ret = null; string ext = Path.GetExtension(fileName).ToLower(); switch (ext) { case ".ws": using (FileStream fs = File.OpenRead(fileName)) { BinaryFormatter binFormatter = new BinaryFormatter(); ret = (TagDoc)binFormatter.Deserialize(fs); fs.Close(); } break; case ".xml": default: ret = new TagDoc(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNode findNode = null; findNode = xmlDoc.DocumentElement.SelectSingleNode(@"/document[@id]"); if (findNode != null) { ret.id = findNode.Attributes["id"].Value; } else { return(null); } findNode = xmlDoc.DocumentElement.SelectSingleNode(@"/document/doc"); if (findNode != null) { ret.content = findNode.InnerText; } else { return(null); } findNode = xmlDoc.DocumentElement.SelectSingleNode(@"/document/title"); if (findNode != null) { ret.title = findNode.InnerText; } findNode = xmlDoc.DocumentElement.SelectSingleNode(@"/document/author"); if (findNode != null) { ret.author = findNode.InnerText; } findNode = xmlDoc.DocumentElement.SelectSingleNode(@"/document/meta[@url]"); if (findNode != null) { ret.url = findNode.Attributes["url"].Value; } break; } if (ret != null) { if (ret.historyList == null) { ret.historyList = new List <HistoryNode>(); } HistoryNode newHistoryNode = new HistoryNode(); newHistoryNode.loadDateTime = DateTime.Now; newHistoryNode.saveDateTime = DateTime.Now; newHistoryNode.computerName = System.Environment.MachineName; ret.historyList.Add(newHistoryNode); } return(ret); }