Example #1
0
        public static XDocument LoadFile([NotNull] string filename, XmlReaderSettings settings, XmlParserContext context)
        {
            if (filename == null)
            {
                throw new ArgumentNullException(nameof(filename));
            }
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("File not found", filename);
            }

            XDocument         document;
            XmlReaderSettings options = settings ?? XmlReaderHelper.CreateSettings();

            using (XmlReader reader = context == null ? XmlReader.Create(filename, options) : XmlReader.Create(filename, options, context))
                document = XDocument.Load(reader, LoadOptions.SetBaseUri);

            return(document);
        }
Example #2
0
        public static XmlDocument ParseXml(string value, bool ignoreWhitespace, bool ignoreComments, params string[] namespacesUri)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            XmlReaderSettings settings   = XmlReaderHelper.CreateSettings(ignoreWhitespace, ignoreComments, Constants.XML_VALIDATE_EVERYTHING_FLAGS, ValidationType.Schema);
            XmlDocument       document   = XmlDocumentHelper.Create(ignoreWhitespace, namespacesUri);
            TextReader        textReader = null;
            XmlReader         reader     = null;

            try
            {
                textReader = new StringReader(value);
                reader     = XmlReader.Create(textReader, settings, document.NameTable.CreateParserContext(ignoreWhitespace));

                while (reader.Read())
                {
                    ReadNode(reader, document, ignoreComments);
                }
            }
            catch
            {
                document = null;
            }
            finally
            {
                if (reader != null && reader.ReadState != ReadState.Closed)
                {
                    reader.Close();
                    ObjectHelper.Dispose(ref reader);
                    ObjectHelper.Dispose(ref textReader);
                }
            }

            return(document);