4using System.Runtime.ExceptionServices;
6using System.Threading.Tasks;
78 else if (ContentType.StartsWith(
"application/") && ContentType.EndsWith(
"+xml"))
100 public Task<ContentResponse>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding,
101 KeyValuePair<string, string>[] Fields, Uri BaseUri,
ICodecProgress Progress)
105 if (Encoding is
null)
107 Doc =
new XmlDocument()
109 PreserveWhitespace =
true
112 using (MemoryStream ms =
new MemoryStream(Data))
114 XmlReaderSettings Settings =
new XmlReaderSettings()
116 CheckCharacters =
false,
117 ConformanceLevel = ConformanceLevel.Document,
118 DtdProcessing = DtdProcessing.Ignore,
119 IgnoreComments =
true,
120 IgnoreProcessingInstructions =
true,
121 IgnoreWhitespace =
false
124 using (XmlReader xr = XmlReader.Create(ms, Settings))
138 catch (XmlException ex)
142 Log.
Warning(
"Invalid XML was received and repaired.",
143 new KeyValuePair<string, object>(
"BaseUri", BaseUri?.OriginalString),
144 new KeyValuePair<string, object>(
"Error", ex.Message));
159 switch (FileExtension.ToLower())
170 ContentType =
string.Empty;
183 ContentType = ContentType.ToLower();
185 switch (ContentType.ToLower())
188 FileExtension =
"xml";
192 FileExtension =
"xsd";
196 if (ContentType.StartsWith(
"application/") && ContentType.EndsWith(
"+xml"))
198 FileExtension = ContentType.Substring(12, ContentType.Length - 4 - 12);
203 FileExtension =
string.Empty;
244 public Task<ContentResponse>
EncodeAsync(
object Object, Encoding Encoding,
ICodecProgress Progress, params
string[] AcceptedContentTypes)
248 if (Object is XmlDocument Doc)
250 else if (Object is XmlElement E)
252 Doc =
new XmlDocument();
253 Doc.AppendChild(Doc.ImportNode(E,
true));
268 return Task.FromResult(
new ContentResponse(
new ArgumentException(
"Unable to encode object, or content type not accepted.", nameof(Object))));
278 public static Task<ContentResponse>
EncodeXmlAsync(XmlDocument Xml, Encoding Encoding,
string ContentType)
280 MemoryStream ms =
null;
281 XmlWriterSettings Settings;
287 ms =
new MemoryStream();
290 if (Encoding is
null)
292 Settings.Encoding = Encoding.UTF8;
293 ContentType +=
"; charset=utf-8";
297 Settings.Encoding = Encoding;
298 ContentType +=
"; charset=" + Encoding.WebName;
301 w = XmlWriter.Create(ms, Settings);
306 Result = ms.ToArray();
324 public static Task<ContentResponse>
EncodeXmlAsync(
string Xml, Encoding Encoding,
string ContentType)
328 if (Encoding is
null)
330 ContentType +=
"; charset=utf-8";
331 Bin = Encoding.UTF8.GetBytes(Xml);
335 ContentType +=
"; charset=" + Encoding.WebName;
336 Bin = Encoding.GetBytes(Xml);
Contains information about a response to a content request.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
string[] ContentTypes
Supported content types.
static readonly string[] XmlContentTypes
XML content types.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
static readonly string[] XmlFileExtensions
XML file extensions.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
const string DefaultContentType
Default content type for XML documents.
string[] FileExtensions
Supported file extensions.
const string SchemaContentType
Default content type for XML schema documents.
static Task< ContentResponse > EncodeXmlAsync(XmlDocument Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
static Task< ContentResponse > EncodeXmlAsync(string Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
XmlCodec()
XML encoder/decoder.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Helps with common XML-related tasks.
static string Encode(string s)
Encodes a string for use in XML.
static string RepairXml(string Xml)
Repairs broker XML by reencoding any illegal characters.
static XmlWriterSettings WriterSettings(bool Indent, bool OmitXmlDeclaration)
Gets an XML writer settings object.
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Static class managing the application event log. Applications and services log events on this static ...
static void Warning(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a warning event.
Static class managing binary representations of strings.
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...