Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 26 additions & 35 deletions openTRANS/XmlCreator.cs → openTRANS/Xml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,48 @@
using System.Text;
using System.Xml.Serialization;

namespace openTRANS
{
public class XmlCreator
{
namespace openTRANS {
public static class Xml {
//private readonly string xmlVersion = "1.0";
//private readonly Encoding encoding = new UpperCaseUTF8Encoding(); //Previously Encoding.Utf8; in case of changing also change StringWriter below; would otherwise produce wrong xml encoding attribute
//private readonly bool standalone = true;

//private readonly XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

private readonly Order order;

public XmlCreator(Order order) => this.order = order;

public string Result
{
get
{
//var xdoc = new XDocument(
// new XDeclaration(xmlVersion, encoding.WebName, standalone ? "yes" : "no")
// );
static Xml() {
}

public static string Serialize<T>(T o) where T : class, new() {

var xmlns = new XmlSerializerNamespaces();
xmlns.Add(nameof(Common.Namespace.xsi), Common.Namespace.xsi);
xmlns.Add(nameof(Common.Namespace.bmecat), Common.Namespace.bmecat);
xmlns.Add(nameof(Common.Namespace.xmime), Common.Namespace.xmime);
xmlns.Add(nameof(Common.Namespace.xsig), Common.Namespace.xsi);
var xmlns = new XmlSerializerNamespaces();
xmlns.Add(nameof(Common.Namespace.xsi), Common.Namespace.xsi);
xmlns.Add(nameof(Common.Namespace.bmecat), Common.Namespace.bmecat);
xmlns.Add(nameof(Common.Namespace.xmime), Common.Namespace.xmime);
xmlns.Add(nameof(Common.Namespace.xsig), Common.Namespace.xsi);


XmlSerializer serializer = new XmlSerializer(typeof(Order));
using (var writer = new UTF8StringWriter())
{
serializer.Serialize(writer, order, xmlns);
return writer.ToString();
}
XmlSerializer serializer = new XmlSerializer(typeof(o));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof(o) is wrong here..

Use typeof(T)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh… I see… fixed

using (var writer = new UTF8StringWriter()) {
serializer.Serialize(writer, o, xmlns);
return writer.ToString();
}
}

public static T Deserialize<T>(string xml) {
var serializer = new XmlSerializer(typeof(T));

using (TextReader reader = new StringReader(xml)) {
return (T)serializer.Deserialize(reader);
}
}
//https://stackoverflow.com/questions/3871738/force-xdocument-to-write-to-string-with-utf-8-encoding
private class Utf8StringWriter : StringWriter
{
private class Utf8StringWriter : StringWriter {
public override Encoding Encoding { get { return Encoding.UTF8; } }
}

//writes UTF-8 in uppercase; see class below
private class UTF8StringWriter : StringWriter
{
private class UTF8StringWriter : StringWriter {
public override Encoding Encoding { get { return new UpperCaseUTF8Encoding(); } }
}

Expand All @@ -58,15 +52,12 @@ private class UTF8StringWriter : StringWriter
/// Sometimes utf-8 definition in first line needed in uppercase by suppliers.
/// https://stackoverflow.com/questions/4291332/utf-8-in-uppercase
/// </summary>
public class UpperCaseUTF8Encoding : UTF8Encoding
{
public class UpperCaseUTF8Encoding : UTF8Encoding {

public override string WebName => base.WebName.ToUpper();

public static UpperCaseUTF8Encoding UpperCaseUTF8
{
get
{
public static UpperCaseUTF8Encoding UpperCaseUTF8 {
get {
if (upperCaseUtf8Encoding == null)
upperCaseUtf8Encoding = new UpperCaseUTF8Encoding();
return upperCaseUtf8Encoding;
Expand Down
33 changes: 0 additions & 33 deletions openTRANS/XmlReader.cs

This file was deleted.