EDI Tools for .NET provides EDI templates that are regular C# classes, therefore, any functionality in .NET for XML serialization and deserialization is applicable to the EDI templates.
EDI Tools for .NET is a .NET library that allows you to:
- Read the contents of an EDI file into POCOs, which are instances of EDI templates.
- Write the contents of POCOs into EDI files.
Learn How to serialize and deserialize (marshal and unmarshal) XML in .NET in this Microsoft MSDN article. All guides in the article are applicable to the EDI template POCOs.
The quickest method of converting between XML text and a .NET object is using the XmlSerializer. All EDI templates are marked for XML serialization, e.g. all classes and properties are annotated with the [Serializable()] attribute.
How to serialize EDI to XML
This example uses Microsoft's XmlSerializer. The destination XML structure matches that of the EDI template and expected by the serializer. Read this article to learn Control XML serialization using attributes from Microsoft MSDN.
-
// Read EDI file List ediItems; using (var ediReader = new X12Reader(ediStream, factory)) ediItems = ediReader.ReadToEnd().ToList(); // Pull all EDI objects var transactions = ediItems.OfType(); // Serialize each EDI object to XML foreach (var transaction in transactions) var xml = transaction.Serialize();
-
var transaction = new TS850(); // Populate the transaction and serialize it to JSON var xml = transaction.Serialize();
This is an example implementation of the Serialize method:
public static XDocument Serialize(EdiMessage instance)
{
if (instance == null)
throw new ArgumentNullException("instance");
var serializer = new XmlSerializer(instance.GetType());
using (var ms = new MemoryStream())
{
serializer.Serialize(ms, instance);
ms.Position = 0;
return XDocument.Load(ms, LoadOptions.PreserveWhitespace);
}
}
Examples in GitHub:
How to deserialize EDI from XML
This example uses Microsoft's XmlSerializer. In order to create an EDI POCO from XML, your XML must match the structure expected by the serializer. Read this article to learn Control XML serialization using attributes from Microsoft MSDN.
// Read XML string
var ediXml = XElement.Load(ediStream);
// Deserialize to EDI object
var transaction = ediXml.Deserialize();
This is an example implementation of the Deserialize method:
public static T Deserialize(XElement xml)
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(xml.CreateReader());
}
Examples in GitHub:
Sample XML String
The XML produced by serialization with Microsoft's XmlSerializer looks like this:
<TS850 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ST>
<TransactionSetIdentifierCode_01>850</TransactionSetIdentifierCode_01>
<TransactionSetControlNumber_02>0001</TransactionSetControlNumber_02>
</ST>
<BEG>
<TransactionSetPurposeCode_01>00</TransactionSetPurposeCode_01>
<PurchaseOrderTypeCode_02>SA</PurchaseOrderTypeCode_02>
<PurchaseOrderNumber_03>XX-1234</PurchaseOrderNumber_03>
<Date_05>20170301</Date_05>
<AcknowledgmentType_07>NA</AcknowledgmentType_07>
</BEG>
<PER>
<PER>
<ContactFunctionCode_01>BD</ContactFunctionCode_01>
<Name_02>ED SMITH</Name_02>
<CommunicationNumberQualifier_03>TE</CommunicationNumberQualifier_03>
<CommunicationNumber_04>8001234567</CommunicationNumber_04>
</PER>
</PER>
<TAX>
<TAX>
<TaxIdentificationNumber_01>53247765</TaxIdentificationNumber_01>
<LocationQualifier_02>SP</LocationQualifier_02>
<LocationIdentifier_03>CA</LocationIdentifier_03>
<TaxExemptCode_12>9</TaxExemptCode_12>
</TAX>
</TAX>
<N1Loop>
<Loop_N1_850>
<N1>
<EntityIdentifierCode_01>BY</EntityIdentifierCode_01>
<Name_02>ABC AEROSPACE</Name_02>
<IdentificationCodeQualifier_03>9</IdentificationCodeQualifier_03>
<IdentificationCode_04>1234567890101</IdentificationCode_04>
</N1>
<N2>
<N2>
<Name_01>AIRCRAFT DIVISION</Name_01>
</N2>
</N2>
<N3>
<N3>
<AddressInformation_01>2000 JET BLVD</AddressInformation_01>
</N3>
</N3>
<N4>
<N4>
<CityName_01>FIGHTER TOWN</CityName_01>
<StateorProvinceCode_02>CA</StateorProvinceCode_02>
<PostalCode_03>98898</PostalCode_03>
</N4>
</N4>
</Loop_N1_850>
</N1Loop>
<PO1Loop>
<Loop_PO1_850>
<PO1>
<AssignedIdentification_01>1</AssignedIdentification_01>
<QuantityOrdered_02>25</QuantityOrdered_02>
<UnitorBasisforMeasurementCode_03>EA</UnitorBasisforMeasurementCode_03>
<UnitPrice_04>36</UnitPrice_04>
<BasisofUnitPriceCode_05>PE</BasisofUnitPriceCode_05>
<ProductServiceIDQualifier_06>MG</ProductServiceIDQualifier_06>
<ProductServiceID_07>XYZ-1234</ProductServiceID_07>
</PO1>
<MEA>
<MEA>
<MeasurementReferenceIDCode_01>WT</MeasurementReferenceIDCode_01>
<MeasurementQualifier_02>WT</MeasurementQualifier_02>
<MeasurementValue_03>10</MeasurementValue_03>
<CompositeUnitofMeasure_04>
<UnitorBasisforMeasurementCode_01>OZ</UnitorBasisforMeasurementCode_01>
</CompositeUnitofMeasure_04>
</MEA>
</MEA>
<IT8>
<ProductServiceSubstitutionCode_07>B0</ProductServiceSubstitutionCode_07>
</IT8>
<SCHLoop>
<Loop_SCH_850>
<SCH>
<Quantity_01>25</Quantity_01>
<UnitorBasisforMeasurementCode_02>EA</UnitorBasisforMeasurementCode_02>
<DateTimeQualifier_05>106</DateTimeQualifier_05>
<Date_06>20170615</Date_06>
</SCH>
</Loop_SCH_850>
</SCHLoop>
</Loop_PO1_850>
</PO1Loop>
<CTTLoop>
<CTT>
<NumberofLineItems_01>1</NumberofLineItems_01>
</CTT>
<AMT>
<AmountQualifierCode_01>TT</AmountQualifierCode_01>
<MonetaryAmount_02>900</MonetaryAmount_02>
</AMT>
</CTTLoop>
<SE>
<NumberofIncludedSegments_01>15</NumberofIncludedSegments_01>
<TransactionSetControlNumber_02>0001</TransactionSetControlNumber_02>
</SE>
</TS850>
EDI template to XSD
Sometimes it is required to convert the EDI templates to XSDs, for example, if you are applying XSL transformations. XSDs can be generated from compiled assemblies containing the desired EDI templates. Use Microsoft's XSD.exe to generate the XSDs.
For example, if you need an XSD for purchase order 850, version 4010, do the following:
- Create a library project and name it "MyXsdProject"
- Add the purchase order EDI template to it (EDI Tools for .NET Tutorial).
- Build the project to generate the DLL MyXsdProject.dll
-
Start Developer Command Prompt and type in:
xsd.exe MyXsdProject.dll /type:EdiFabric.Templates.X12004010.TS850
EDI control segments to XSD
To generate XSDs for the control or acknowledgment segments use the EdiFabric.dll assembly.
-
xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.X12.ISA xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.X12.IEA xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.X12.GS xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.X12.GE xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.X12.TA1
-
xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.Edifact.UNB xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.Edifact.UNG xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.Edifact.UNE xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.Edifact.UNZ
-
xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Hl7.FHS xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Hl7.BHS xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Hl7.BTS xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Hl7.FTS
-
xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Telco.TransactionHeader xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Telco.TransactionHeader13 xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Telco.ResponseHeader xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Telco.ResponseHeader13
-
xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.Ncpdp.UIB xsd.exe EdiFabric.dll /type:EdiFabric.Core.Model.Edi.Ncpdp.UIZ
Comments
0 comments
Please sign in to leave a comment.