Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ oi.ProductPriceFix.PriceAmount = 14;
//oi.ProductPriceFix.AllowOrChargesFix.AllowOrCharge.Type = AllowOrChargeTypes.Allowance;

o.OrderItemList.Add(oi);


string xml = Xml.Serialize<Order>(o);
}

var ot = new XmlCreator(o).Result;
OrderResponse response = Xml.Deserialize<OrderResponse>(serializedXmlData);
```

# Links
- Website https://www.dxsdata.com/2019/08/opentrans-for-net-core/
- Website https://www.dxsdata.com/2019/08/opentrans-for-net-core/
4 changes: 4 additions & 0 deletions openTRANS/IOpenTransBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace openTRANS {
public interface IOpenTransBase {
}
}
2 changes: 1 addition & 1 deletion openTRANS/ORDER/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions openTRANS/ORDERRESPONSE/OrderResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -26,4 +26,4 @@ public OrderResponseSummary OrderResponseSummary
set { }
}
}
}
}
63 changes: 63 additions & 0 deletions openTRANS/Xml.cs
Original file line number Diff line number Diff line change
@@ -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>(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<T>(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(); } }
}


/// <summary>
/// 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 override string WebName => base.WebName.ToUpper();

public static UpperCaseUTF8Encoding UpperCaseUTF8 {
get {
if (upperCaseUtf8Encoding == null)
upperCaseUtf8Encoding = new UpperCaseUTF8Encoding();
return upperCaseUtf8Encoding;
}
}

private static UpperCaseUTF8Encoding upperCaseUtf8Encoding = null;
}
}
}
79 changes: 0 additions & 79 deletions openTRANS/XmlCreator.cs

This file was deleted.

33 changes: 0 additions & 33 deletions openTRANS/XmlReader.cs

This file was deleted.

5 changes: 2 additions & 3 deletions testform/testform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrderResponse>(textBox2.Text);
}
}
}