Exemple #1
0
        /// <summary>
        /// Load an Xml document to an XmlDocument object
        /// </summary>
        /// <param name="fileOrXml">The filepath or string representation of the Xml document</param>
        /// <param name="type">The type to define if fileOrXml is a file path or the Xml string</param>
        /// <returns>A parsed XmlDocument of the Xml document</returns>
        public static XmlDocument LoadXmlDocument(string fileOrXml, XmlLoadType type)
        {
            XmlDocument doc = new XmlDocument();

            try
            {
                switch (type)
                {
                case XmlLoadType.FromFile:
                    doc.Load(fileOrXml);
                    break;

                case XmlLoadType.FromString:
                    doc.LoadXml(fileOrXml);
                    break;
                }
            }
            catch (XmlException xmlEx)
            {
                if (File.Exists(fileOrXml))
                {
                    Logging.Exception("Failed to load Xml file: {0}\n{1}", Path.GetFileName(fileOrXml), xmlEx.ToString());
                }
                else
                {
                    Logging.Exception("failed to load Xml string:\n" + xmlEx.ToString());
                }
                return(null);
            }
            return(doc);
        }
Exemple #2
0
        /// <summary>
        /// Load an Xml document to an XDocument object
        /// </summary>
        /// <param name="fileOrXml">The filepath or string representation of the Xml document</param>
        /// <param name="type">The type to define if fileOrXml is a file path or the Xml string</param>
        /// <returns>A parsed XDocument of the Xml document</returns>
        public static XDocument LoadXDocument(string fileOrXml, XmlLoadType type)
        {
            if (type == XmlLoadType.FromFile && !File.Exists(fileOrXml))
            {
                Logging.Error("XmlLoadType set to file and file does not exist at {0}", fileOrXml);
                return(null);
            }
            try
            {
                switch (type)
                {
                case XmlLoadType.FromFile:
                    return(XDocument.Load(fileOrXml, LoadOptions.SetLineInfo));

                case XmlLoadType.FromString:
                    return(XDocument.Parse(fileOrXml, LoadOptions.SetLineInfo));
                }
            }
            catch (XmlException ex)
            {
                Logging.Exception(ex.ToString());
                return(null);
            }
            return(null);
        }