2using System.Collections.Generic;
5using System.Threading.Tasks;
75 else if (ContentType.StartsWith(
"application/") && ContentType.EndsWith(
"+xml"))
97 public Task<object>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
99 XmlDocument Doc =
new XmlDocument()
101 PreserveWhitespace =
true
104 if (Encoding is
null)
106 using (MemoryStream ms =
new MemoryStream(Data))
117 return Task.FromResult<
object>(Doc);
128 switch (FileExtension.ToLower())
139 ContentType =
string.Empty;
152 ContentType = ContentType.ToLower();
154 switch (ContentType.ToLower())
157 FileExtension =
"xml";
161 FileExtension =
"xsd";
165 if (ContentType.StartsWith(
"application/") && ContentType.EndsWith(
"+xml"))
167 FileExtension = ContentType.Substring(12, ContentType.Length - 4 - 12);
172 FileExtension =
string.Empty;
213 public Task<KeyValuePair<byte[], string>>
EncodeAsync(
object Object, Encoding Encoding, params
string[] AcceptedContentTypes)
217 if (Object is XmlDocument Doc)
219 else if (Object is XmlElement E)
221 Doc =
new XmlDocument();
222 Doc.AppendChild(Doc.ImportNode(E,
true));
237 throw new ArgumentException(
"Unable to encode object, or content type not accepted.", nameof(Object));
247 public static Task<KeyValuePair<byte[], string>>
EncodeXmlAsync(XmlDocument Xml, Encoding Encoding,
string ContentType)
249 MemoryStream ms =
null;
250 XmlWriterSettings Settings;
256 ms =
new MemoryStream();
259 if (Encoding is
null)
261 Settings.Encoding = Encoding.UTF8;
262 ContentType +=
"; charset=utf-8";
266 Settings.Encoding = Encoding;
267 ContentType +=
"; charset=" + Encoding.WebName;
270 w = XmlWriter.Create(ms, Settings);
275 Result = ms.ToArray();
283 return Task.FromResult(
new KeyValuePair<
byte[],
string>(Result, ContentType));
293 public static Task<KeyValuePair<byte[], string>>
EncodeXmlAsync(
string Xml, Encoding Encoding,
string ContentType)
297 if (Encoding is
null)
299 ContentType +=
"; charset=utf-8";
300 Bin = Encoding.UTF8.GetBytes(Xml);
304 ContentType +=
"; charset=" + Encoding.WebName;
305 Bin = Encoding.GetBytes(Xml);
308 return Task.FromResult(
new KeyValuePair<
byte[],
string>(Bin, ContentType));
Helps with parsing of commong data types.
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
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.
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
static Task< KeyValuePair< byte[], string > > EncodeXmlAsync(XmlDocument Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
string[] ContentTypes
Supported content types.
static readonly string[] XmlContentTypes
XML content types.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
static readonly string[] XmlFileExtensions
XML file extensions.
const string DefaultContentType
Default content type for XML documents.
string[] FileExtensions
Supported file extensions.
static Task< KeyValuePair< byte[], string > > EncodeXmlAsync(string Xml, Encoding Encoding, string ContentType)
Encodes an XML Document.
const string SchemaContentType
Default content type for XML schema documents.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
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 XmlWriterSettings WriterSettings(bool Indent, bool OmitXmlDeclaration)
Gets an XML writer settings object.
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...