示例#1
0
        /// <summary>
        /// Updates the specified XML element with a value for the specified
        /// XML file.
        /// </summary>
        /// <param name="path">The path to the file.</param>
        /// <param name="element">The name of the XML element.</param>
        /// <param name="value">The value to update for the XML element.</param>
        /// <param name="ns">An optional namespace for the XML element. (Set null to ignore)</param>
        /// <returns>A value indicating if the XML element was updated.</returns>
        public bool UpdateElement(string path, string element, string value, XNamespace ns)
        {
            try
            {
                if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(element) || String.IsNullOrEmpty(value))
                {
                    return(false);
                }

                if (!IoFileUtils.FileExists(path))
                {
                    return(false);
                }

                if (IoFileUtils.IsFileReadOnly(path))
                {
                    if (!IoFileUtils.RemoveReadOnlyAttribute(path))
                    {
                        return(false);
                    }
                }

                var contents = IoFileUtils.ReadFileAsString(path);

                if (String.IsNullOrEmpty(contents) || String.IsNullOrWhiteSpace(contents))
                {
                    return(false);
                }

                var xDoc = XDocument.Parse(contents);
                if (xDoc.Root == null)
                {
                    return(false);
                }

                var el = ns != null
                    ? xDoc.Root.Element(ns + element)
                    : xDoc.Root.Element(element);

                //try ignore namespage
                if (ns != null && el == null)
                {
                    el = xDoc.Root.Element(element);
                }

                if (el == null)
                {
                    return(false);
                }

                el.Value = value;

                return(IoFileUtils.DeleteFile(path) && IoFileUtils.CreateFile(path, xDoc.ToString()));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// Gets the value of the specified XML element from the specified XML
        /// file.
        /// </summary>
        /// <param name="path">The path to the file.</param>
        /// <param name="element">The name of the XML element.</param>
        /// <param name="ns">An optional namespace for the XML element. (Set null to ignore)</param>
        /// <returns>The value of the specified XML element.</returns>
        public string GetElement(string path, string element, XNamespace ns)
        {
            try
            {
                var contents = IoFileUtils.ReadFileAsString(path);
                if (String.IsNullOrEmpty(contents) || String.IsNullOrWhiteSpace(contents))
                {
                    return(null);
                }

                var xDoc = XDocument.Parse(contents);
                if (xDoc.Root == null)
                {
                    return(null);
                }

                var el = ns != null
                    ? xDoc.Root.Element(ns + element)
                    : xDoc.Root.Element(element);

                //try ignore namespace
                if (el == null && ns != null)
                {
                    el = xDoc.Root.Element(element);
                }

                return(el != null
                    ? String.IsNullOrEmpty(el.Value)
                        ? null
                        : el.Value
                    : null);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(null);
            }
        }