/// <summary>
        /// Creates a new OpenXmlUnknownElement class by using the outer XML.
        /// </summary>
        /// <param name="outerXml">The outer XML of the element.</param>
        /// <returns>A new OpenXmlUnknownElement class.</returns>
        public static OpenXmlUnknownElement CreateOpenXmlUnknownElement(string outerXml)
        {
            if (String.IsNullOrEmpty(outerXml))
            {
                throw new ArgumentNullException("outerXml");
            }

            TextReader stringReader = new StringReader(outerXml);

            using (XmlReader xmlReader = XmlConvertingReaderFactory.Create(stringReader, OpenXmlElementContext.CreateDefaultXmlReaderSettings()))
            {
                do // O15:#3024890, Skip the leading whitespaces. OpenXmUnknownlElement ignores the Whitespace NodeType.
                {
                    // Fix bug #484153.
                    if (xmlReader.Read() && xmlReader.NodeType == XmlNodeType.Element)
                    {
                        OpenXmlUnknownElement newElement = new OpenXmlUnknownElement(xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
                        newElement.OuterXml = outerXml;
                        return(newElement);
                    }
                } while (xmlReader.NodeType == XmlNodeType.Whitespace);

                // This method always expects an Element NodeType is passed, and there may be one or more preceding Whitespace NodeTypes before the Element.
                // If it's not the case, then throw an exception.
                throw new ArgumentException(ExceptionMessages.InvalidOuterXml, "outerXml");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the OpenXmlMiscNode class using the
        /// supplied XmlNodeType and outer XML values.
        /// </summary>
        /// <param name="nodeType">The XmlNodeType value.</param>
        /// <param name="outerXml">The outer XML of the element.</param>
        public OpenXmlMiscNode(XmlNodeType nodeType, string outerXml)
            : this(nodeType)
        {
            if (string.IsNullOrEmpty(outerXml))
            {
                throw new ArgumentNullException(nameof(outerXml));
            }

            // check the out XML match the nodeType
            using (StringReader stringReader = new StringReader(outerXml))
            {
                XmlReaderSettings settings = new XmlReaderSettings
                {
#if FEATURE_XML_PROHIBIT_DTD
                    ProhibitDtd = true,                     // set true explicitly for security fix
#else
                    DtdProcessing = DtdProcessing.Prohibit, // set to prohibit explicitly for security fix
#endif
                };

                using (XmlReader xmlReader = XmlConvertingReaderFactory.Create(stringReader, settings))
                {
                    xmlReader.Read();

                    if (xmlReader.NodeType != nodeType)
                    {
                        throw new ArgumentException(ExceptionMessages.InvalidOuterXmlForMiscNode);
                    }
                }
            }

            RawOuterXml = outerXml;
        }
        /// <summary>
        /// Initializes a new instance of the OpenXmlMiscNode class using the
        /// supplied XmlNodeType and outer XML values.
        /// </summary>
        /// <param name="nodeType">The XmlNodeType value.</param>
        /// <param name="outerXml">The outer XML of the element.</param>
        public OpenXmlMiscNode(XmlNodeType nodeType, string outerXml)
            : this(nodeType)
        {
            if (String.IsNullOrEmpty(outerXml))
            {
                throw new ArgumentNullException("outerXml");
            }

            // check the out XML match the nodeType
            using (StringReader stringReader = new StringReader(outerXml))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.DtdProcessing = DtdProcessing.Prohibit; // set true explicitly for serucity fix
                using (XmlReader xmlReader = XmlConvertingReaderFactory.Create(stringReader, settings))
                {
                    xmlReader.Read();

                    if (xmlReader.NodeType != nodeType)
                    {
                        throw new ArgumentException(ExceptionMessages.InvalidOuterXmlForMiscNode);
                    }

                    xmlReader.Close();
                }
            }

            this.RawOuterXml = outerXml;
        }
Ejemplo n.º 4
0
        private void Init(Stream partStream, bool closeInput)
        {
            _elementContext.XmlReaderSettings.CloseInput = closeInput;

#if FEATURE_XML_PROHIBIT_DTD
            _elementContext.XmlReaderSettings.ProhibitDtd = true;                     // set true explicitly for security fix
#else
            _elementContext.XmlReaderSettings.DtdProcessing = DtdProcessing.Prohibit; // set to prohibit explicitly for security fix
#endif

            _elementContext.XmlReaderSettings.IgnoreWhitespace = true; // O15:#3024890, the default is false, but we set it to True for compatibility of OpenXmlPartReader behavior
            _xmlReader = XmlConvertingReaderFactory.Create(partStream, _elementContext.XmlReaderSettings);

            _xmlReader.Read();

            if (_xmlReader.NodeType == XmlNodeType.XmlDeclaration)
            {
                _encoding = _xmlReader["encoding"]; // get the "encoding" attribute

                //if (!String.IsNullOrEmpty(encoding))
                //{
                //    try
                //    {
                //        this._encoding = Encoding.GetEncoding(encoding);
                //    }
                //    catch (ArgumentException)
                //    {
                //        // should we catch?
                //        this._encoding = Encoding.UTF8;
                //    }
                //}

                string standalone = _xmlReader["standalone"]; // get the "standalone" attribute

                if (!String.IsNullOrEmpty(standalone))
                {
                    if (standalone == "yes")
                    {
                        _standalone = true;
                    }
                    else
                    {
                        _standalone = false;
                    }
                }
            }

            _elementState = ElementState.Null;
        }
Ejemplo n.º 5
0
        private static XmlReader CreateReader(Stream partStream, bool closeInput, long maxCharactersInPart, out bool?_standalone, out string _encoding)
        {
            var settings = new XmlReaderSettings
            {
                MaxCharactersInDocument = maxCharactersInPart,
                CloseInput       = closeInput,
                IgnoreWhitespace = true,
#if FEATURE_XML_PROHIBIT_DTD
                ProhibitDtd = true,
#else
                DtdProcessing = DtdProcessing.Prohibit,
#endif
            };

            var xmlReader = XmlConvertingReaderFactory.Create(partStream, settings);

            xmlReader.Read();

            if (xmlReader.NodeType == XmlNodeType.XmlDeclaration)
            {
                _encoding = xmlReader["encoding"];

                var standalone = xmlReader["standalone"];

                if (string.Equals("yes", standalone, StringComparison.Ordinal))
                {
                    _standalone = true;
                }
                else if (string.Equals("no", standalone, StringComparison.Ordinal))
                {
                    _standalone = false;
                }
                else
                {
                    _standalone = null;
                }
            }
            else
            {
                _encoding   = null;
                _standalone = null;
            }

            return(xmlReader);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Load the DOM tree from the Open XML part.
        /// </summary>
        /// <param name="openXmlPart">The part this root element to be loaded from.</param>
        /// <param name="partStream">The stream of the part.</param>
        /// <returns>
        /// Returns true when the part stream is loaded successfully into this root element.
        /// Returns false when the part stream does not contain any xml element.
        /// </returns>
        /// <exception cref="InvalidDataException">Thrown when the part stream contains an incorrect root element.</exception>
        internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream)
        {
            Profiler.CommentMarkProfile(Profiler.MarkId.OpenXmlPartRootElement_LoadFromPart_In);

            if (partStream.Length < 4)
            {
                // The XmlReader.Read() method requires at least four bytes from the data stream in order to begin parsing.
                return(false);
            }

            // set MaxCharactersInDocument to limit the part size on loading DOM.
            this.OpenXmlElementContext.XmlReaderSettings.MaxCharactersInDocument = openXmlPart.MaxCharactersInPart;
            this.OpenXmlElementContext.XmlReaderSettings.DtdProcessing           = DtdProcessing.Prohibit; // set true explicitly for security fix

            using (XmlReader xmlReader = XmlConvertingReaderFactory.Create(partStream, this.OpenXmlElementContext.XmlReaderSettings, openXmlPart.OpenXmlPackage.StrictTranslation))
            {
                this.OpenXmlElementContext.MCSettings = openXmlPart.MCSettings;


                xmlReader.Read();

                if (xmlReader.NodeType == XmlNodeType.XmlDeclaration)
                {
                    string standaloneAttribute = xmlReader.GetAttribute("standalone");
                    if (standaloneAttribute != null)
                    {
                        this._standaloneDeclaration = standaloneAttribute.Equals("yes", StringComparison.OrdinalIgnoreCase);
                    }
                }

                if (!xmlReader.EOF)
                {
                    xmlReader.MoveToContent();
                }

                if (xmlReader.EOF ||
                    XmlNodeType.Element != xmlReader.NodeType ||
                    !xmlReader.IsStartElement())
                {
                    //the stream does NOT contains any xml element.
                    return(false);
                }

                byte nsId;

                if (!NamespaceIdMap.TryGetNamespaceId(xmlReader.NamespaceURI, out nsId) ||
                    nsId != this.NamespaceId ||
                    xmlReader.LocalName != this.LocalName)
                {
                    string elementQName = new XmlQualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI).ToString();
                    string msg          = String.Format(System.Globalization.CultureInfo.CurrentUICulture, ExceptionMessages.Fmt_PartRootIsInvalid, elementQName, this.XmlQualifiedName.ToString());
                    throw new InvalidDataException(msg);
                }

                // remove all children and clear all attributes
                this.OuterXml = string.Empty;
                var mcContextPushed = this.PushMcContext(xmlReader);
                this.Load(xmlReader, this.OpenXmlElementContext.LoadMode);
                if (mcContextPushed)
                {
                    this.PopMcContext();
                }
            }

            Profiler.CommentMarkProfile(Profiler.MarkId.OpenXmlPartRootElement_LoadFromPart_Out);

            return(true);
        }
        internal bool LoadFromPart(OpenXmlPart openXmlPart, Stream partStream)
        {
            if (partStream.Length < 4)
            {
                // The XmlReader.Read() method requires at least four bytes from the data stream in order to begin parsing.
                return(false);
            }

            // set MaxCharactersInDocument to limit the part size on loading DOM.
            OpenXmlElementContext.XmlReaderSettings.MaxCharactersInDocument = openXmlPart.MaxCharactersInPart;

#if FEATURE_XML_PROHIBIT_DTD
            OpenXmlElementContext.XmlReaderSettings.ProhibitDtd = true;                     // set true explicitly for security fix
#else
            OpenXmlElementContext.XmlReaderSettings.DtdProcessing = DtdProcessing.Prohibit; // set to prohibit explicitly for security fix
#endif

            using (XmlReader xmlReader = XmlConvertingReaderFactory.Create(partStream, OpenXmlElementContext.XmlReaderSettings, openXmlPart.OpenXmlPackage.StrictRelationshipFound))
            {
                OpenXmlElementContext.MCSettings = openXmlPart.MCSettings;

                xmlReader.Read();

                if (xmlReader.NodeType == XmlNodeType.XmlDeclaration)
                {
                    string standaloneAttribute = xmlReader.GetAttribute("standalone");
                    if (standaloneAttribute is not null)
                    {
                        _standaloneDeclaration = standaloneAttribute.Equals("yes", StringComparison.OrdinalIgnoreCase);
                    }
                }

                if (!xmlReader.EOF)
                {
                    xmlReader.MoveToContent();
                }

                if (xmlReader.EOF ||
                    xmlReader.NodeType != XmlNodeType.Element ||
                    !xmlReader.IsStartElement())
                {
                    //the stream does NOT contains any xml element.
                    return(false);
                }

                if (!NamespaceIdMap.TryGetNamespaceId(xmlReader.NamespaceURI, out byte nsId) ||
                    nsId != NamespaceId ||
                    xmlReader.LocalName != LocalName)
                {
                    var elementQName = new XmlQualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI).ToString();
                    var msg          = SR.Format(ExceptionMessages.Fmt_PartRootIsInvalid, elementQName, XmlQualifiedName.ToString());

                    throw new InvalidDataException(msg);
                }

                // remove all children and clear all attributes
                OuterXml = string.Empty;
                var mcContextPushed = PushMcContext(xmlReader);
                Load(xmlReader, OpenXmlElementContext.LoadMode);
                if (mcContextPushed)
                {
                    PopMcContext();
                }
            }

            return(true);
        }
Ejemplo n.º 8
0
 public static XmlReader Create(TextReader textReader)
 {
     return(XmlConvertingReaderFactory.Create(textReader, true));
 }
Ejemplo n.º 9
0
 public static XmlReader Create(TextReader textReader, XmlReaderSettings settings)
 {
     return(XmlConvertingReaderFactory.Create(textReader, settings, true));
 }
Ejemplo n.º 10
0
 public static XmlReader Create(Stream partStream, XmlReaderSettings settings)
 {
     return(XmlConvertingReaderFactory.Create(partStream, settings, true));
 }