//============================================================ // STATIC METHODS //============================================================ #region ContentFormatAsMimeType(SyndicationContentFormat format) /// <summary> /// Returns the MIME content type for the supplied <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">The <see cref="SyndicationContentFormat"/> to get the MIME content type for.</param> /// <returns>The MIME content type for the supplied <paramref name="format"/>, otherwise returns <b>text/xml</b>.</returns> public static string ContentFormatAsMimeType(SyndicationContentFormat format) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ string contentType = "text/xml"; //------------------------------------------------------------ // Return MIME type based on supplied format //------------------------------------------------------------ foreach (System.Reflection.FieldInfo fieldInfo in typeof(SyndicationContentFormat).GetFields()) { if (fieldInfo.FieldType == typeof(SyndicationContentFormat)) { SyndicationContentFormat contentFormat = (SyndicationContentFormat)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); if (contentFormat == format) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(MimeMediaTypeAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { MimeMediaTypeAttribute mediaType = customAttributes[0] as MimeMediaTypeAttribute; string mimeType = String.Format(null, "{0}/{1}", mediaType.Name, mediaType.SubName); contentType = mimeType; break; } } } } return(contentType); }
/// <summary> /// Returns the <see cref="SyndicationContentFormat"/> enumeration value that corresponds to the specified MIME content type. /// </summary> /// <param name="contentType">The MIME type of the syndication format.</param> /// <returns>A <see cref="SyndicationContentFormat"/> enumeration value that corresponds to the specified string, otherwise returns <b>SyndicationContentFormat.None</b>.</returns> /// <remarks>This method disregards case of specified MIME content type.</remarks> /// <exception cref="ArgumentNullException">The <paramref name="contentType"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="contentType"/> is an empty string.</exception> public static SyndicationContentFormat ContentFormatByMimeType(string contentType) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ SyndicationContentFormat contentFormat = SyndicationContentFormat.None; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNullOrEmptyString(contentType, "contentType"); //------------------------------------------------------------ // Determine syndication content format based on supplied name //------------------------------------------------------------ foreach (System.Reflection.FieldInfo fieldInfo in typeof(SyndicationContentFormat).GetFields()) { if (fieldInfo.FieldType == typeof(SyndicationContentFormat)) { SyndicationContentFormat format = (SyndicationContentFormat)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(MimeMediaTypeAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { MimeMediaTypeAttribute mediaType = customAttributes[0] as MimeMediaTypeAttribute; string mimeType = String.Format(null, "{0}/{1}", mediaType.Name, mediaType.SubName); if (String.Compare(contentType, mimeType, StringComparison.OrdinalIgnoreCase) == 0) { contentFormat = format; break; } } } } return(contentFormat); }