| | | 1 | | using System.Net.Mime; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Xml.Serialization; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Http.ContentWriters; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Creates a <see cref="HttpContent"/> object for XML types. |
| | | 9 | | /// </summary> |
| | | 10 | | public class XmlContentFactory : IHttpContentFactory |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc /> |
| | 41 | 13 | | public IEnumerable<string> SupportedContentTypes => new[] |
| | 41 | 14 | | { |
| | 41 | 15 | | MediaTypeNames.Application.Xml, |
| | 41 | 16 | | MediaTypeNames.Text.Xml, |
| | 41 | 17 | | MediaTypeNames.Application.Soap, |
| | 41 | 18 | | }; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public HttpContent CreateHttpContent(object content, string contentType) |
| | | 22 | | { |
| | 2 | 23 | | var text = content as string ?? Serialize(content); |
| | 2 | 24 | | return new RawStringContent(text, Encoding.UTF8, contentType); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private string Serialize(object value) |
| | | 28 | | { |
| | 0 | 29 | | using var writer = new StringWriter(); |
| | 0 | 30 | | new XmlSerializer(value.GetType()).Serialize(writer, value); |
| | 0 | 31 | | return writer.ToString(); |
| | 0 | 32 | | } |
| | | 33 | | } |