/// <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> /// Returns an <see cref="Encoding"/> that represents the XML character encoding for the supplied <see cref="Stream"/>. /// </summary> /// <param name="stream">A <see cref="Stream"/> that represents an XML data source to determine the character encoding for.</param> /// <returns> /// A <see cref="Encoding"/> that represents the character encoding specified by the XML data source. /// If the character encoding is not specified or unable to be determined, returns <see cref="Encoding.UTF8"/>. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> public static Encoding GetXmlEncoding(Stream stream) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(stream, "stream"); //------------------------------------------------------------ // Read the stream contents and return character encoding //------------------------------------------------------------ using (StreamReader reader = new StreamReader(stream)) { return(SyndicationEncodingUtility.GetXmlEncoding(reader.ReadToEnd())); } }
//puke ///// <summary> ///// Extracts the character encoding for the content type of the supplied <see cref="HttpRequest"/>. ///// </summary> ///// <param name="request">The HTTP values sent by a client during a Web request.</param> ///// <returns> ///// A <see cref="Encoding"/> that represents character encoding of the Content-Type <i>charset</i> attribute. ///// If the <i>charset</i> attribute is unavailable or invalid, returns <b>null</b>. ///// </returns> ///// <exception cref="ArgumentNullException">The <paramref name="request"/> is a null reference (Nothing in Visual Basic).</exception> //public static Encoding GetCharacterEncoding(HttpRequest request) //{ // //------------------------------------------------------------ // // Local members // //------------------------------------------------------------ // Encoding contentEncoding = null; // //------------------------------------------------------------ // // Validate parameter // //------------------------------------------------------------ // Guard.ArgumentNotNull(request, "request"); // //------------------------------------------------------------ // // Extract character encoding from HTTP request // //------------------------------------------------------------ // if (!String.IsNullOrEmpty(request.ContentType)) // { // if (request.ContentType.Contains(";")) // { // string[] contentTypeParts = request.ContentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); // if (contentTypeParts != null && contentTypeParts.Length > 0) // { // for (int i = 0; i < contentTypeParts.Length; i++) // { // string typePart = contentTypeParts[i].Trim(); // if (typePart.Contains("=")) // { // string[] nameValuePair = typePart.Split("=".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); // if (nameValuePair != null && nameValuePair.Length == 2) // { // string name = nameValuePair[0].Trim(); // string value = nameValuePair[1].Trim(); // if (String.Compare(name, "charset", StringComparison.OrdinalIgnoreCase) == 0) // { // try // { // contentEncoding = Encoding.GetEncoding(value); // } // catch (ArgumentException) // { // return null; // } // } // } // } // } // } // } // } // return contentEncoding; //} #endregion #region GetXmlEncoding(byte[] data) /// <summary> /// Returns an <see cref="Encoding"/> that represents the XML character encoding for the supplied array of bytes. /// </summary> /// <param name="data">An array of bytes that represents an XML data source to determine the character encoding for.</param> /// <returns> /// A <see cref="Encoding"/> that represents the character encoding specified by the XML data source. /// If the character encoding is not specified or unable to be determined, returns <see cref="Encoding.UTF8"/>. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="data"/> is a null reference (Nothing in Visual Basic).</exception> public static Encoding GetXmlEncoding(byte[] data) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(data, "data"); //------------------------------------------------------------ // Create stream using data and return character encoding //------------------------------------------------------------ using (MemoryStream stream = new MemoryStream(data)) { return(SyndicationEncodingUtility.GetXmlEncoding(stream)); } }