Beispiel #1
0
        /// <summary>
        /// Performs shared initialization between Load and LoadAsync.
        /// </summary>
        static XDocument InitLoad(XmlReader reader, LoadOptions options)
        {
            XDocument d = new XDocument();

            if ((options & LoadOptions.SetBaseUri) != 0)
            {
                string baseUri = reader.BaseURI;
                if (!string.IsNullOrEmpty(baseUri))
                {
                    d.SetBaseUri(baseUri);
                }
            }
            if ((options & LoadOptions.SetLineInfo) != 0)
            {
                IXmlLineInfo li = reader as IXmlLineInfo;
                if (li != null && li.HasLineInfo())
                {
                    d.SetLineInfo(li.LineNumber, li.LinePosition);
                }
            }
            if (reader.NodeType == XmlNodeType.XmlDeclaration)
            {
                d.Declaration = new XDeclaration(reader);
            }
            return(d);
        }
Beispiel #2
0
        /// <summary>
        /// Create a new <see cref="XDocument"/> containing the contents of the
        /// passed in <see cref="XmlReader"/>.
        /// </summary>
        /// <param name="reader">
        /// An <see cref="XmlReader"/> containing the XML to be read into the new
        /// <see cref="XDocument"/>.
        /// </param>
        /// <param name="options">
        /// A set of <see cref="LoadOptions"/>.
        /// </param>
        /// <returns>
        /// A new <see cref="XDocument"/> containing the contents of the passed
        /// in <see cref="XmlReader"/>.
        /// </returns>
        public static XDocument Load(XmlReader reader, LoadOptions options)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (reader.ReadState == ReadState.Initial)
            {
                reader.Read();
            }
            XDocument d = new XDocument();

            if ((options & LoadOptions.SetBaseUri) != 0)
            {
                string baseUri = reader.BaseURI;
                if (baseUri != null && baseUri.Length != 0)
                {
                    d.SetBaseUri(baseUri);
                }
            }
            if ((options & LoadOptions.SetLineInfo) != 0)
            {
                IXmlLineInfo li = reader as IXmlLineInfo;
                if (li != null && li.HasLineInfo())
                {
                    d.SetLineInfo(li.LineNumber, li.LinePosition);
                }
            }
            if (reader.NodeType == XmlNodeType.XmlDeclaration)
            {
                d.Declaration = new XDeclaration(reader);
            }
            d.ReadContentFrom(reader, options);
            if (!reader.EOF)
            {
                throw new InvalidOperationException(SR.InvalidOperation_ExpectedEndOfFile);
            }
            if (d.Root == null)
            {
                throw new InvalidOperationException(SR.InvalidOperation_MissingRoot);
            }
            return(d);
        }
Beispiel #3
0
        public static XDocument Load(XmlReader reader, LoadOptions options)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (reader.ReadState == System.Xml.ReadState.Initial)
            {
                reader.Read();
            }
            XDocument document = new XDocument();

            if ((options & LoadOptions.SetBaseUri) != LoadOptions.None)
            {
                string baseURI = reader.BaseURI;
                if ((baseURI != null) && (baseURI.Length != 0))
                {
                    document.SetBaseUri(baseURI);
                }
            }
            if ((options & LoadOptions.SetLineInfo) != LoadOptions.None)
            {
                IXmlLineInfo info = reader as IXmlLineInfo;
                if ((info != null) && info.HasLineInfo())
                {
                    document.SetLineInfo(info.LineNumber, info.LinePosition);
                }
            }
            if (reader.NodeType == XmlNodeType.XmlDeclaration)
            {
                document.Declaration = new XDeclaration(reader);
            }
            document.ReadContentFrom(reader, options);
            if (!reader.EOF)
            {
                throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_ExpectedEndOfFile"));
            }
            if (document.Root == null)
            {
                throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_MissingRoot"));
            }
            return(document);
        }