Пример #1
0
        /// <summary>
        /// Load a document from a file.
        /// </summary>
        /// <param name="file">The path of the file.</param>
        /// <returns></returns>
        public static Document LoadXmlFile(string file)
        {
            XDocument       xdoc = XDocument.Load(file);
            List <Category> cats = new List <Category>();

            bool malformed = false;

            foreach (XElement ele in xdoc.Root.Elements("category"))
            {
                Category c = LoadCategory(ele);

                if (c != null) // null check to avoid malformed files
                {
                    cats.Add(c);
                }
                else
                {
                    malformed = true;
                }
            }

            Base.Properties p   = new Base.Properties();
            XElement        elp = xdoc.Root.Element("properties");

            if (elp != null)
            {
                p.Product           = elp.Attribute("product")?.Value ?? "";
                p.Author            = elp.Attribute("author")?.Value ?? "";
                p.Language          = elp.Attribute("lang")?.Value ?? "";
                p.Translator        = elp.Attribute("tlr")?.Value ?? "";
                p.TranslatorContact = elp.Attribute("tlrcontact")?.Value ?? "";
                p.Description       = elp.Attribute("desc")?.Value ?? "";
            }

            Document d = new Document
            {
                Filename             = file,
                Categories           = cats,
                Properties           = p,
                HadMalformedElements = malformed
            };

            return(d);
        }
Пример #2
0
        /// <summary>
        /// Save a document to a file.
        /// </summary>
        /// <param name="file">The path to create the file at.</param>
        /// <param name="doc">The data of the document.</param>
        public static void SaveXmlFile(string file, Document doc)
        {
            XDocument xdoc = new XDocument();
            XElement  root = new XElement("root");

            if (doc.Properties != null)
            {
                Base.Properties p = doc.Properties;

                XElement prop = new XElement("properties");
                prop.SetAttributeValue("product", p.Product);
                prop.SetAttributeValue("author", p.Author);
                prop.SetAttributeValue("lang", p.Language);
                prop.SetAttributeValue("tlr", p.Translator);
                prop.SetAttributeValue("tlrcontact", p.TranslatorContact);
                prop.SetAttributeValue("desc", p.Description);
            }

            foreach (Category cat in doc.Categories)
            {
                XElement cel = new XElement("category");
                cel.SetAttributeValue("name", cat.Name);

                foreach (Translation item in cat.Translations)
                {
                    XElement tre = new XElement("item");
                    tre.SetAttributeValue("id", item.Id);
                    tre.SetAttributeValue("note", item.Note);
                    tre.SetValue(item.TranslatedItem);

                    cel.Add(tre);
                }

                root.Add(cel);
            }

            xdoc.Add(root);
            xdoc.Save(file);
        }