Example #1
0
        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);
        }
Example #2
0
        //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);
        }