Пример #1
0
        public static XDocument LoadXml(string filePath)
        {
            XDocument doc = null;

            ToolBox.IsProperFilenameCase(filePath);

            if (File.Exists(filePath))
            {
                try
                {
                    using FileStream stream = File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    using XmlReader reader  = CreateReader(stream);
                    doc = XDocument.Load(reader);
                }
                catch
                {
                    return(null);
                }

                if (doc.Root == null)
                {
                    return(null);
                }
            }

            return(doc);
        }
Пример #2
0
        public static XDocument TryLoadXml(string filePath)
        {
            XDocument doc;

            try
            {
                ToolBox.IsProperFilenameCase(filePath);
                using FileStream stream = File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                using XmlReader reader  = CreateReader(stream);
                doc = XDocument.Load(reader);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Couldn't load xml document \"" + filePath + "\"!", e);
                return(null);
            }
            if (doc?.Root == null)
            {
                DebugConsole.ThrowError("File \"" + filePath + "\" could not be loaded: Document or the root element is invalid!");
                return(null);
            }
            return(doc);
        }