diff --git a/README.md b/README.md index 20d2d9d..864de9c 100644 --- a/README.md +++ b/README.md @@ -99,10 +99,13 @@ oi.ProductPriceFix.PriceAmount = 14; //oi.ProductPriceFix.AllowOrChargesFix.AllowOrCharge.Type = AllowOrChargeTypes.Allowance; o.OrderItemList.Add(oi); + + +string xml = Xml.Serialize(o); } -var ot = new XmlCreator(o).Result; +OrderResponse response = Xml.Deserialize(serializedXmlData); ``` # Links -- Website https://www.dxsdata.com/2019/08/opentrans-for-net-core/ \ No newline at end of file +- Website https://www.dxsdata.com/2019/08/opentrans-for-net-core/ diff --git a/openTRANS/IOpenTransBase.cs b/openTRANS/IOpenTransBase.cs new file mode 100644 index 0000000..3af226e --- /dev/null +++ b/openTRANS/IOpenTransBase.cs @@ -0,0 +1,4 @@ +namespace openTRANS { + public interface IOpenTransBase { + } +} diff --git a/openTRANS/ORDER/Order.cs b/openTRANS/ORDER/Order.cs index 38a0583..ba750cd 100644 --- a/openTRANS/ORDER/Order.cs +++ b/openTRANS/ORDER/Order.cs @@ -6,7 +6,7 @@ namespace openTRANS { [XmlRoot("ORDER", Namespace = "http://www.opentrans.org/XMLSchema/2.1", IsNullable = false)] - public partial class Order + public partial class Order : IOpenTransBase { [XmlAttribute("schemaLocation", Namespace = XmlSchema.InstanceNamespace)] public string xsiSchemaLocation = "http://www.opentrans.org/XMLSchema/2.1%20opentrans_2_1.xsd"; diff --git a/openTRANS/ORDERRESPONSE/OrderResponse.cs b/openTRANS/ORDERRESPONSE/OrderResponse.cs index 98790fc..3d74cca 100644 --- a/openTRANS/ORDERRESPONSE/OrderResponse.cs +++ b/openTRANS/ORDERRESPONSE/OrderResponse.cs @@ -4,7 +4,7 @@ namespace openTRANS { [XmlRoot("ORDERRESPONSE", Namespace = "http://www.opentrans.org/XMLSchema/2.1", IsNullable = false)] - public partial class OrderResponse + public partial class OrderResponse : IOpenTransBase { [XmlAttribute("version")] public string Version = "2.1"; @@ -26,4 +26,4 @@ public OrderResponseSummary OrderResponseSummary set { } } } -} \ No newline at end of file +} diff --git a/openTRANS/Xml.cs b/openTRANS/Xml.cs new file mode 100644 index 0000000..28cea4c --- /dev/null +++ b/openTRANS/Xml.cs @@ -0,0 +1,63 @@ +using System.IO; +using System.Text; +using System.Xml.Serialization; + +namespace openTRANS { + public static class Xml { + static Xml() { + } + + public static string Serialize(T o) where T : class, IOpenTransBase { + + 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(T)); + using (var writer = new UTF8StringWriter()) { + serializer.Serialize(writer, o, xmlns); + return writer.ToString(); + } + } + + public static T Deserialize(string xml) where T : class, IOpenTransBase { + 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 { + public override Encoding Encoding { get { return Encoding.UTF8; } } + } + + //writes UTF-8 in uppercase; see class below + private class UTF8StringWriter : StringWriter { + public override Encoding Encoding { get { return new UpperCaseUTF8Encoding(); } } + } + + + /// + /// Sometimes utf-8 definition in first line needed in uppercase by suppliers. + /// https://stackoverflow.com/questions/4291332/utf-8-in-uppercase + /// + public class UpperCaseUTF8Encoding : UTF8Encoding { + + public override string WebName => base.WebName.ToUpper(); + + public static UpperCaseUTF8Encoding UpperCaseUTF8 { + get { + if (upperCaseUtf8Encoding == null) + upperCaseUtf8Encoding = new UpperCaseUTF8Encoding(); + return upperCaseUtf8Encoding; + } + } + + private static UpperCaseUTF8Encoding upperCaseUtf8Encoding = null; + } + } +} diff --git a/openTRANS/XmlCreator.cs b/openTRANS/XmlCreator.cs deleted file mode 100644 index 5487781..0000000 --- a/openTRANS/XmlCreator.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System.IO; -using System.Text; -using System.Xml.Serialization; - -namespace openTRANS -{ - public class XmlCreator - { - //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") - // ); - - - 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(); - } - } - } - - //https://stackoverflow.com/questions/3871738/force-xdocument-to-write-to-string-with-utf-8-encoding - private class Utf8StringWriter : StringWriter - { - public override Encoding Encoding { get { return Encoding.UTF8; } } - } - - //writes UTF-8 in uppercase; see class below - private class UTF8StringWriter : StringWriter - { - public override Encoding Encoding { get { return new UpperCaseUTF8Encoding(); } } - } - - - /// - /// Sometimes utf-8 definition in first line needed in uppercase by suppliers. - /// https://stackoverflow.com/questions/4291332/utf-8-in-uppercase - /// - public class UpperCaseUTF8Encoding : UTF8Encoding - { - - public override string WebName => base.WebName.ToUpper(); - - public static UpperCaseUTF8Encoding UpperCaseUTF8 - { - get - { - if (upperCaseUtf8Encoding == null) - upperCaseUtf8Encoding = new UpperCaseUTF8Encoding(); - return upperCaseUtf8Encoding; - } - } - - private static UpperCaseUTF8Encoding upperCaseUtf8Encoding = null; - } - } -} diff --git a/openTRANS/XmlReader.cs b/openTRANS/XmlReader.cs deleted file mode 100644 index 0da4d88..0000000 --- a/openTRANS/XmlReader.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.IO; -using System.Xml.Serialization; - -namespace openTRANS -{ - public class XmlReader - { - private string orderResponse; - - - public XmlReader(string orderResponse) - { - this.orderResponse = orderResponse; - } - - - public OrderResponse Result - { - get - { - var serializer = new XmlSerializer(typeof(OrderResponse)); - OrderResponse orderResponse; - - using (TextReader reader = new StringReader(this.orderResponse)) - { - orderResponse = (OrderResponse)serializer.Deserialize(reader); - } - return orderResponse; - } - } - - } -} diff --git a/testform/testform.cs b/testform/testform.cs index 5ec9ed1..15d6f98 100644 --- a/testform/testform.cs +++ b/testform/testform.cs @@ -137,12 +137,11 @@ private void testform_Load(object sender, EventArgs e) o.OrderItemList.Add(oi); } - var ot = new XmlCreator(o); - textBox1.Text = ot.Result; + textBox1.Text = Xml.Serialize(o); } private void textBox2_TextChanged(object sender, EventArgs e) { - OrderResponse response = new XmlReader(textBox2.Text).Result; + OrderResponse response = Xml.Deserialize(textBox2.Text); } } }