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"; BinaryFormatter binFormatter = new BinaryFormatter(); binFormatter.Serialize(File.Create(objFileName), savedDoc); 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(); XmlWriter xw = XmlWriter.Create(File.Create(fileName)); xw.Settings.Indent = true; xw.Settings.IndentChars = "\t"; xw.Settings.Encoding = Encoding.UTF8; xmlDoc.WriteTo(xw); return(true); }
//static functions here public static TagDoc LoadFile(string fileName) { if (!File.Exists(fileName)) { return(null); } TagDoc 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; } return(ret); }
private void btOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofDlg = new OpenFileDialog(); ofDlg.DefaultExt = ".xml"; ofDlg.Filter = "XML File|*.xml|All File|*.*"; ofDlg.Multiselect = false; if (ofDlg.ShowDialog() == true) { curDoc = AppBase.LoadFile(ofDlg.FileName); if (curDoc == null) { MessageBox.Show("Failed to load seleted file, check file format."); } else { rTBDoc.SelectAll(); rTBDoc.Selection.Text = curDoc.content; rTBDoc.Selection.Select(rTBDoc.Document.ContentStart, rTBDoc.Document.ContentStart); meTagControl.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); } } }