EDI to XML

Exporting or importing EDI to or from XML

Converting EDI to XML is seamless by using .NET XML Serializer or .NET DataContractSerializer. All EDI templates are marked for XML serialization.

1. Serialize EDI to XML using XmlSerializer

//  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();

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);
    }
}

 

 

 


 

 

 

2. Deserialize to EDI from XML using XmlSerializer

// Read XML string
var ediXml = XElement.Load(ediStream);

// Deserialize to EDI object
var transaction = ediXml.Deserialize();

public static T Deserialize(XElement xml)
{
    var serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(xml.CreateReader());
}

 

 

 


 

 

 

Sample XML String

<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>

 

 

 


 

 

 

3. Serialize EDI to XML using DataContractSerializer

public static XDocument Serialize(EdiMessage instance)
{
    if (instance == null)
    throw new ArgumentNullException("instance");

    var serializer = new DataContractSerializer(instance.GetType());
    using (var ms = new MemoryStream())
    {
        serializer.WriteObject(ms, instance);
        ms.Position = 0;
        return XDocument.Load(ms, LoadOptions.PreserveWhitespace);
    }
}

 

 

 


 

 

 

4. Deserialize to EDI from XML using DataContractSerializer

public static T Deserialize(XElement xml)
{
    var serializer = new DataContractSerializer(typeof(T));
    return (T)serializer.ReadObject(xml.CreateReader());
}

 

 

 


 

 

 

Sample XML String

<TS850 xmlns="http://schemas.datacontract.org/2004/07/EdiFabric.Templates.X12004010" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
	<ErrorContext xmlns="http://schemas.datacontract.org/2004/07/EdiFabric.Core.Model.Edi" xmlns:a="http://schemas.datacontract.org/2004/07/EdiFabric.Core.Model.Edi.ErrorContexts">
		<a:Message i:nil="true" />
		<a:Codes xmlns:b="http://schemas.datacontract.org/2004/07/EdiFabric.Core.ErrorCodes" />
		<a:ControlNumber>0001</a:ControlNumber>
		<a:Errors />
		<a:Name>850</a:Name>
		<a:Version>004010</a:Version>
	</ErrorContext>
	<ADVLoop i:nil="true" />
	<AMTLoop i:nil="true" />
	<BEG>
		<AcknowledgmentType_07>NA</AcknowledgmentType_07>
		<ContractNumber_06 i:nil="true" />
		<ContractTypeCode_09 i:nil="true" />
		<Date_05>20170301</Date_05>
		<InvoiceTypeCode_08 i:nil="true" />
		<PurchaseCategory_10 i:nil="true" />
		<PurchaseOrderNumber_03>XX-1234</PurchaseOrderNumber_03>
		<PurchaseOrderTypeCode_02>SA</PurchaseOrderTypeCode_02>
		<ReleaseNumber_04 i:nil="true" />
		<SecurityLevelCode_11 i:nil="true" />
		<TransactionSetPurposeCode_01>00</TransactionSetPurposeCode_01>
		<TransactionTypeCode_12 i:nil="true" />
	</BEG>
	<CSH i:nil="true" />
	<CTB i:nil="true" />
	<CTP i:nil="true" />
	<CTTLoop>
		<AMT>
			<AmountQualifierCode_01>TT</AmountQualifierCode_01>
			<CreditDebitFlagCode_03 i:nil="true" />
			<MonetaryAmount_02>900</MonetaryAmount_02>
		</AMT>
		<CTT>
			<Description_07 i:nil="true" />
			<HashTotal_02 i:nil="true" />
			<NumberofLineItems_01>1</NumberofLineItems_01>
			<UnitorBasisforMeasurementCode_04 i:nil="true" />
			<UnitorBasisforMeasurementCode_06 i:nil="true" />
			<Volume_05 i:nil="true" />
			<Weight_03 i:nil="true" />
		</CTT>
	</CTTLoop>
	<CUR i:nil="true" />
	<DIS i:nil="true" />
	<DTM i:nil="true" />
	<FOB i:nil="true" />
	<INC i:nil="true" />
	<ITD i:nil="true" />
	<LDT i:nil="true" />
	<LIN i:nil="true" />
	<LMLoop i:nil="true" />
	<MAN i:nil="true" />
	<MEA i:nil="true" />
	<N1Loop>
		<Loop_N1_850>
			<FOB i:nil="true" />
			<N1>
				<EntityIdentifierCode_01>BY</EntityIdentifierCode_01>
				<EntityIdentifierCode_06 i:nil="true" />
				<EntityRelationshipCode_05 i:nil="true" />
				<IdentificationCodeQualifier_03>9</IdentificationCodeQualifier_03>
				<IdentificationCode_04>1234567890101</IdentificationCode_04>
				<Name_02>ABC AEROSPACE</Name_02>
			</N1>
			<N2>
				<N2>
					<Name_01>AIRCRAFT DIVISION</Name_01>
					<Name_02 i:nil="true" />
				</N2>
			</N2>
			<N3>
				<N3>
					<AddressInformation_01>2000 JET BLVD</AddressInformation_01>
					<AddressInformation_02 i:nil="true" />
				</N3>
			</N3>
			<N4>
				<N4>
					<CityName_01>FIGHTER TOWN</CityName_01>
					<CountryCode_04 i:nil="true" />
					<LocationIdentifier_06 i:nil="true" />
					<LocationQualifier_05 i:nil="true" />
					<PostalCode_03>98898</PostalCode_03>
					<StateorProvinceCode_02>CA</StateorProvinceCode_02>
				</N4>
			</N4>
			<NX2 i:nil="true" />
			<PER i:nil="true" />
			<PKG i:nil="true" />
			<REF i:nil="true" />
			<SI i:nil="true" />
			<TD1 i:nil="true" />
			<TD3 i:nil="true" />
			<TD4 i:nil="true" />
			<TD5 i:nil="true" />
		</Loop_N1_850>
	</N1Loop>
	<N9Loop i:nil="true" />
	<PAM i:nil="true" />
	<PCT i:nil="true" />
	<PER>
		<PER>
			<CommunicationNumberQualifier_03>TE</CommunicationNumberQualifier_03>
			<CommunicationNumberQualifier_05 i:nil="true" />
			<CommunicationNumberQualifier_07 i:nil="true" />
			<CommunicationNumber_04>8001234567</CommunicationNumber_04>
			<CommunicationNumber_06 i:nil="true" />
			<CommunicationNumber_08 i:nil="true" />
			<ContactFunctionCode_01>BD</ContactFunctionCode_01>
			<ContactInquiryReference_09 i:nil="true" />
			<Name_02>ED SMITH</Name_02>
		</PER>
	</PER>
	<PID i:nil="true" />
	<PKG i:nil="true" />
	<PO1Loop>
		<Loop_PO1_850>
			<AMTLoop i:nil="true" />
			<CN1 i:nil="true" />
			<CSH i:nil="true" />
			<CTB i:nil="true" />
			<CTPLoop i:nil="true" />
			<CUR i:nil="true" />
			<DIS i:nil="true" />
			<DTM i:nil="true" />
			<FOB i:nil="true" />
			<INC i:nil="true" />
			<IT3 i:nil="true" />
			<IT8>
				<AccountNumber_04 i:nil="true" />
				<ActionCode_02 i:nil="true" />
				<AgencyQualifierCode_06 i:nil="true" />
				<Amount_03 i:nil="true" />
				<Date_05 i:nil="true" />
				<ProductServiceIDQualifier_08 i:nil="true" />
				<ProductServiceIDQualifier_10 i:nil="true" />
				<ProductServiceIDQualifier_12 i:nil="true" />
				<ProductServiceIDQualifier_14 i:nil="true" />
				<ProductServiceIDQualifier_16 i:nil="true" />
				<ProductServiceIDQualifier_18 i:nil="true" />
				<ProductServiceIDQualifier_20 i:nil="true" />
				<ProductServiceIDQualifier_22 i:nil="true" />
				<ProductServiceIDQualifier_24 i:nil="true" />
				<ProductServiceIDQualifier_26 i:nil="true" />
				<ProductServiceID_09 i:nil="true" />
				<ProductServiceID_11 i:nil="true" />
				<ProductServiceID_13 i:nil="true" />
				<ProductServiceID_15 i:nil="true" />
				<ProductServiceID_17 i:nil="true" />
				<ProductServiceID_19 i:nil="true" />
				<ProductServiceID_21 i:nil="true" />
				<ProductServiceID_23 i:nil="true" />
				<ProductServiceID_25 i:nil="true" />
				<ProductServiceID_27 i:nil="true" />
				<ProductServiceSubstitutionCode_07>B0</ProductServiceSubstitutionCode_07>
				<SalesRequirementCode_01 i:nil="true" />
			</IT8>
			<ITD i:nil="true" />
			<LIN i:nil="true" />
			<LMLoop i:nil="true" />
			<LSLoop i:nil="true" />
			<MAN i:nil="true" />
			<MEA>
				<MEA>
					<CompositeUnitofMeasure_04>
						<Exponent_02 i:nil="true" />
						<Exponent_05 i:nil="true" />
						<Exponent_08 i:nil="true" />
						<Exponent_11 i:nil="true" />
						<Exponent_14 i:nil="true" />
						<Multiplier_03 i:nil="true" />
						<Multiplier_06 i:nil="true" />
						<Multiplier_09 i:nil="true" />
						<Multiplier_12 i:nil="true" />
						<Multiplier_15 i:nil="true" />
						<UnitorBasisforMeasurementCode_01>OZ</UnitorBasisforMeasurementCode_01>
						<UnitorBasisforMeasurementCode_04 i:nil="true" />
						<UnitorBasisforMeasurementCode_07 i:nil="true" />
						<UnitorBasisforMeasurementCode_10 i:nil="true" />
						<UnitorBasisforMeasurementCode_13 i:nil="true" />
					</CompositeUnitofMeasure_04>
					<MeasurementAttributeCode_08 i:nil="true" />
					<MeasurementMethodorDevice_10 i:nil="true" />
					<MeasurementQualifier_02>WT</MeasurementQualifier_02>
					<MeasurementReferenceIDCode_01>WT</MeasurementReferenceIDCode_01>
					<MeasurementSignificanceCode_07 i:nil="true" />
					<MeasurementValue_03>10</MeasurementValue_03>
					<RangeMaximum_06 i:nil="true" />
					<RangeMinimum_05 i:nil="true" />
					<SurfaceLayerPositionCode_09 i:nil="true" />
				</MEA>
			</MEA>
			<MSG i:nil="true" />
			<N1Loop i:nil="true" />
			<N9Loop i:nil="true" />
			<PAM i:nil="true" />
			<PCT i:nil="true" />
			<PER i:nil="true" />
			<PIDLoop i:nil="true" />
			<PKGLoop i:nil="true" />
			<PO1>
				<AssignedIdentification_01>1</AssignedIdentification_01>
				<BasisofUnitPriceCode_05>PE</BasisofUnitPriceCode_05>
				<ProductServiceIDQualifier_06>MG</ProductServiceIDQualifier_06>
				<ProductServiceIDQualifier_08 i:nil="true" />
				<ProductServiceIDQualifier_10 i:nil="true" />
				<ProductServiceIDQualifier_12 i:nil="true" />
				<ProductServiceIDQualifier_14 i:nil="true" />
				<ProductServiceIDQualifier_16 i:nil="true" />
				<ProductServiceIDQualifier_18 i:nil="true" />
				<ProductServiceIDQualifier_20 i:nil="true" />
				<ProductServiceIDQualifier_22 i:nil="true" />
				<ProductServiceIDQualifier_24 i:nil="true" />
				<ProductServiceID_07>XYZ-1234</ProductServiceID_07>
				<ProductServiceID_09 i:nil="true" />
				<ProductServiceID_11 i:nil="true" />
				<ProductServiceID_13 i:nil="true" />
				<ProductServiceID_15 i:nil="true" />
				<ProductServiceID_17 i:nil="true" />
				<ProductServiceID_19 i:nil="true" />
				<ProductServiceID_21 i:nil="true" />
				<ProductServiceID_23 i:nil="true" />
				<ProductServiceID_25 i:nil="true" />
				<QuantityOrdered_02>25</QuantityOrdered_02>
				<UnitPrice_04>36</UnitPrice_04>
				<UnitorBasisforMeasurementCode_03>EA</UnitorBasisforMeasurementCode_03>
			</PO1>
			<PO3 i:nil="true" />
			<PO4 i:nil="true" />
			<PWK i:nil="true" />
			<QTYLoop i:nil="true" />
			<REF i:nil="true" />
			<SACLoop i:nil="true" />
			<SCHLoop>
				<Loop_SCH_850>
					<REF i:nil="true" />
					<SCH>
						<AssignedIdentification_12 i:nil="true" />
						<DateTimeQualifier_05>106</DateTimeQualifier_05>
						<DateTimeQualifier_08 i:nil="true" />
						<Date_06>20170615</Date_06>
						<Date_09 i:nil="true" />
						<EntityIdentifierCode_03 i:nil="true" />
						<Name_04 i:nil="true" />
						<Quantity_01>25</Quantity_01>
						<RequestReferenceNumber_11 i:nil="true" />
						<Time_07 i:nil="true" />
						<Time_10 i:nil="true" />
						<UnitorBasisforMeasurementCode_02>EA</UnitorBasisforMeasurementCode_02>
					</SCH>
					<TD1 i:nil="true" />
					<TD3 i:nil="true" />
					<TD4 i:nil="true" />
					<TD5 i:nil="true" />
				</Loop_SCH_850>
			</SCHLoop>
			<SDQ i:nil="true" />
			<SI i:nil="true" />
			<SLNLoop i:nil="true" />
			<SPI i:nil="true" />
			<TAX i:nil="true" />
			<TC2 i:nil="true" />
			<TD1 i:nil="true" />
			<TD3 i:nil="true" />
			<TD4 i:nil="true" />
			<TD5 i:nil="true" />
			<TXI i:nil="true" />
		</Loop_PO1_850>
	</PO1Loop>
	<PWK i:nil="true" />
	<REF i:nil="true" />
	<SACLoop i:nil="true" />
	<SE xmlns:a="http://schemas.datacontract.org/2004/07/EdiFabric.Core.Model.Edi.X12">
		<a:NumberofIncludedSegments_01>15</a:NumberofIncludedSegments_01>
		<a:TransactionSetControlNumber_02>0001</a:TransactionSetControlNumber_02>
	</SE>
	<SI i:nil="true" />
	<SPILoop i:nil="true" />
	<ST xmlns:a="http://schemas.datacontract.org/2004/07/EdiFabric.Core.Model.Edi.X12">
		<a:ImplementationConventionPreference_03 i:nil="true" />
		<a:TransactionSetControlNumber_02>0001</a:TransactionSetControlNumber_02>
		<a:TransactionSetIdentifierCode_01>850</a:TransactionSetIdentifierCode_01>
	</ST>
	<TAX>
		<TAX>
			<CustomsEntryTypeGroupCode_13 i:nil="true" />
			<LocationIdentifier_03>CA</LocationIdentifier_03>
			<LocationIdentifier_05 i:nil="true" />
			<LocationIdentifier_07 i:nil="true" />
			<LocationIdentifier_09 i:nil="true" />
			<LocationIdentifier_11 i:nil="true" />
			<LocationQualifier_02>SP</LocationQualifier_02>
			<LocationQualifier_04 i:nil="true" />
			<LocationQualifier_06 i:nil="true" />
			<LocationQualifier_08 i:nil="true" />
			<LocationQualifier_10 i:nil="true" />
			<TaxExemptCode_12>9</TaxExemptCode_12>
			<TaxIdentificationNumber_01>53247765</TaxIdentificationNumber_01>
		</TAX>
	</TAX>
	<TC2 i:nil="true" />
	<TD1 i:nil="true" />
	<TD3 i:nil="true" />
	<TD4 i:nil="true" />
	<TD5 i:nil="true" />
	<TXI i:nil="true" />
</TS850>
Was this article helpful?
2 out of 6 found this helpful