/// <summary> /// Creates a <see cref="XPathNavigator"/> against the supplied <see cref="Stream"/>. /// </summary> /// <param name="stream">The <see cref="Stream"/> object that contains the XML data to be navigated by the created <see cref="XPathNavigator"/>.</param> /// <returns> /// An <see cref="XPathNavigator"/> that provides a cursor model for navigating the supplied <paramref name="stream"/>. /// The supplied <paramref name="stream"/> XML data is parsed to remove invalid XML characters that would normally prevent /// a navigator from being created. /// </returns> /// <remarks> /// The character encoding of the supplied <paramref name="stream"/> is automatically determined based on the <i>encoding</i> attribute of the XML document declaration. /// If the character encoding cannot be determined, a default encoding of <see cref="Encoding.UTF8"/> is used. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> public static XPathNavigator CreateSafeNavigator(Stream stream) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ Encoding encoding = Encoding.UTF8; byte[] buffer = null; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(stream, "stream"); //------------------------------------------------------------ // Extract stream data as an array of bytes //------------------------------------------------------------ buffer = SyndicationEncodingUtility.GetStreamBytes(stream); //------------------------------------------------------------ // Determine encoding of the XML data //------------------------------------------------------------ encoding = SyndicationEncodingUtility.GetXmlEncoding(buffer); //------------------------------------------------------------ // Attempt to return navigator for supplied stream //------------------------------------------------------------ using (MemoryStream memoryStream = new MemoryStream(buffer)) { return(SyndicationEncodingUtility.CreateSafeNavigator(memoryStream, encoding)); } }
/// <summary> /// Creates a <see cref="XPathNavigator"/> against the supplied <see cref="Stream"/>. /// </summary> /// <param name="stream">The <see cref="Stream"/> object that contains the XML data to be navigated by the created <see cref="XPathNavigator"/>.</param> /// <returns> /// An <see cref="XPathNavigator"/> that provides a cursor model for navigating the supplied <paramref name="stream"/>. /// The supplied <paramref name="stream"/> XML data is parsed to remove invalid XML characters that would normally prevent /// a navigator from being created. /// </returns> /// <remarks> /// The character encoding of the supplied <paramref name="stream"/> is automatically determined based on the <i>encoding</i> attribute of the XML document declaration. /// If the character encoding cannot be determined, a default encoding of <see cref="Encoding.UTF8"/> is used. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> public static XPathNavigator CreateSafeNavigator(Stream stream) { Encoding encoding = Encoding.UTF8; byte[] buffer = null; Guard.ArgumentNotNull(stream, "stream"); buffer = SyndicationEncodingUtility.GetStreamBytes(stream); encoding = SyndicationEncodingUtility.GetXmlEncoding(buffer); using (MemoryStream memoryStream = new MemoryStream(buffer)) { return(SyndicationEncodingUtility.CreateSafeNavigator(memoryStream, encoding)); } }