Пример #1
0
        /// <summary>
        /// Creates and returns a chunked sequence of <see cref="XmlReader"/> objects with a maximum of the specified <paramref name="size"/> of XML node elements located on a depth of 1.
        /// </summary>
        /// <param name="reader">The <see cref="XmlReader"/> object that contains the XML data to chunk into smaller <see cref="XmlReader"/> objects for a batch run or similar.</param>
        /// <param name="size">The amount of XML node elements allowed per <see cref="XmlReader"/> object. Default is 128 XML node element.</param>
        /// <param name="setup">The <see cref="XmlWriterSettings"/> which need to be configured.</param>
        /// <returns>An sequence of <see cref="XmlReader"/> objects that contains no more than the specified <paramref name="size"/> of XML node elements from the <paramref name="reader" /> object.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="reader"/> is null.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The <see cref="XmlReader.Read"/> method of the <paramref name="reader"/> object has already been called.
        /// </exception>
        public static IEnumerable <XmlReader> Chunk(XmlReader reader, int size, Action <XmlWriterSettings> setup = null)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (reader.ReadState != ReadState.Initial)
            {
                throw new ArgumentException("The Read method of the XmlReader object has already been called.", nameof(reader));
            }
            List <XmlReader>  outerReaders   = new List <XmlReader>();
            XmlReaderSettings readerSettings = reader.Settings;

            if (MoveToFirstElement(reader))
            {
                XmlQualifiedEntity rootElement  = new XmlQualifiedEntity(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                List <XmlReader>   innerReaders = new List <XmlReader>();
                Stream             result;
                while (reader.Read())
                {
                    if (reader.Depth > 1)
                    {
                        continue;
                    }
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        XPathDocument  document  = new XPathDocument(reader.ReadSubtree());
                        XPathNavigator navigator = document.CreateNavigator();
                        innerReaders.Add(navigator.ReadSubtree());
                        break;
                    }

                    if (innerReaders.Count != size)
                    {
                        continue;
                    }

                    result = XmlWriterUtility.CreateXml(ChunkCore, innerReaders, rootElement, setup);
                    outerReaders.Add(XmlReader.Create(result, readerSettings));
                    innerReaders.Clear();
                }

                if (innerReaders.Count > 0)
                {
                    result = XmlWriterUtility.CreateXml(ChunkCore, innerReaders, rootElement, setup);
                    outerReaders.Add(XmlReader.Create(result, readerSettings));
                    innerReaders.Clear();
                }
            }
            return(outerReaders);
        }
Пример #2
0
        /// <summary>
        /// Remove the namespace declarations from the specified <see cref="Stream"/> <paramref name="value"/>.
        /// </summary>
        /// <param name="value">An XML <see cref="Stream"/> to purge namespace declarations from.</param>
        /// <param name="omitXmlDeclaration">if set to <c>true</c> omit the XML declaration; otherwise <c>false</c>. The default is false.</param>
        /// <param name="encoding">The text encoding to use.</param>
        /// <returns>A <see cref="Stream"/> object representing the specified <paramref name="value"/> but with no namespace declarations.</returns>
        public static Stream RemoveNamespaceDeclarations(Stream value, bool omitXmlDeclaration, Encoding encoding)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            IXPathNavigable navigable = XPathNavigableConverter.FromStream(value, true); // todo: leaveStreamOpen
            XPathNavigator  navigator = navigable.CreateNavigator();
            MemoryStream    output;
            MemoryStream    tempOutput = null;

            try
            {
                tempOutput = new MemoryStream();
                using (XmlWriter writer = XmlWriter.Create(tempOutput, XmlWriterUtility.CreateSettings(o =>
                {
                    o.Encoding = encoding;
                    o.OmitXmlDeclaration = omitXmlDeclaration;
                })))
                {
                    WriteElements(navigator, writer);
                    writer.Flush();
                }
                output          = tempOutput;
                output.Position = 0;
                tempOutput      = null;
            }
            finally
            {
                if (tempOutput != null)
                {
                    tempOutput.Dispose();
                }
            }
            return(output);
        }
Пример #3
0
 /// <summary>
 /// Copies the specified <paramref name="reader"/> using the specified delegate <paramref name="copier"/> and returns the result as an XML stream.
 /// </summary>
 /// <typeparam name="T">The type of the parameter of the delegate <paramref name="copier"/>.</typeparam>
 /// <param name="reader">The <see cref="XmlReader"/> object that contains the XML data.</param>
 /// <param name="copier">The delegate that will create an in-memory copy of <paramref name="reader"/> as a XML stream.</param>
 /// <param name="arg">The parameter of the delegate <paramref name="copier"/>.</param>
 /// <param name="setup">The <see cref="XmlCopyOptions"/> which need to be configured.</param>
 /// <returns>A <see cref="Stream"/> holding the XML copied by the delegate <paramref name="copier"/> from the source <paramref name="reader"/>.</returns>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="reader"/> is null - or - <paramref name="copier"/> is null.
 /// </exception>
 public static Stream Copy <T>(this XmlReader reader, Action <XmlWriter, XmlReader, T> copier, T arg, Action <XmlCopyOptions> setup = null)
 {
     return(XmlWriterUtility.Copy(reader, copier, arg, setup));
 }
Пример #4
0
 /// <summary>
 /// Copies the specified <paramref name="reader"/> using the specified delegate <paramref name="copier"/> and returns the result as an XML stream.
 /// </summary>
 /// <typeparam name="T1">The type of the first parameter of the delegate <paramref name="copier"/>.</typeparam>
 /// <typeparam name="T2">The type of the second parameter of the delegate <paramref name="copier"/>.</typeparam>
 /// <typeparam name="T3">The type of the third parameter of the delegate <paramref name="copier"/>.</typeparam>
 /// <param name="reader">The <see cref="XmlReader"/> object that contains the XML data.</param>
 /// <param name="copier">The delegate that will create an in-memory copy of <paramref name="reader"/> as a XML stream.</param>
 /// <param name="arg1">The first parameter of the delegate <paramref name="copier"/>.</param>
 /// <param name="arg2">The second parameter of the delegate <paramref name="copier"/>.</param>
 /// <param name="arg3">The third parameter of the delegate <paramref name="copier"/>.</param>
 /// <param name="setup">The <see cref="XmlCopyOptions"/> which need to be configured.</param>
 /// <returns>A <see cref="Stream"/> holding the XML copied by the delegate <paramref name="copier"/> from the source <paramref name="reader"/>.</returns>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="reader"/> is null - or - <paramref name="copier"/> is null.
 /// </exception>
 public static Stream Copy <T1, T2, T3>(this XmlReader reader, Action <XmlWriter, XmlReader, T1, T2, T3> copier, T1 arg1, T2 arg2, T3 arg3, Action <XmlCopyOptions> setup = null)
 {
     return(XmlWriterUtility.Copy(reader, copier, arg1, arg2, arg3, setup));
 }
Пример #5
0
 /// <summary>
 /// Copies everything from the specified <paramref name="reader"/> and returns the result as an XML stream.
 /// </summary>
 /// <param name="reader">The <see cref="XmlReader"/> object that contains the XML data.</param>
 /// <param name="setup">The <see cref="XmlCopyOptions"/> which need to be configured.</param>
 /// <returns>A <see cref="Stream"/> holding an exact copy of the source <paramref name="reader"/>.</returns>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="reader"/> is null.
 /// </exception>
 public static Stream Copy(this XmlReader reader, Action <XmlCopyOptions> setup = null)
 {
     return(XmlWriterUtility.Copy(reader, setup));
 }