I am attempting to build an EDI 855 file via the EdiFabric.Definitions.X12_004010_855 class purchased through EdiFabric. I am running into issues, trying to convert this object to a valid EDI 855 file. Here's what I'm doing:
Create new M_855 object:
var m_855 = new M_855();
m_855.S_ST = new EdiFabric.Definitions.X12_004010_855.S_ST();
m_855.S_ST.D_329_2 = "Stuff...";
m_855.S_ST.D_143_1 = EdiFabric.Definitions.X12_004010_855.S_STD_143_1.Item855;
m_855.S_BAK = new EdiFabric.Definitions.X12_004010_855.S_BAK();
....more code, but removed for brevity...
Serialize object into XML
var xml = Serialize<M_855>(m_855, "www.edifabric.com/x12")
var interchange = Interchange.LoadFrom(xml); //blows up here
public static XElement Serialize<T>(T instance, string nameSpace)
{
// Fix: using instance.GetType() instead of typeof(T)
var serializer = new XmlSerializer(instance.GetType(), nameSpace);
using (var ms = new MemoryStream())
{
serializer.Serialize(ms, instance);
ms.Position = 0;
return XElement.Load(ms, LoadOptions.PreserveWhitespace);
}
}
Which gives me this valid XML:
<M_855 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="www.edifabric.com/x12">
<S_ST>
<D_143_1>855</D_143_1>
<D_329_2>1126</D_329_2>
</S_ST>
<S_BAK>
<D_353_1>06</D_353_1>
<D_587_2>AD</D_587_2>
<D_324_3>TEST3</D_324_3>
<D_373_4>20160902</D_373_4>
</S_BAK>
<G_N1>
<S_N1>
<D_98_1>ST</D_98_1>
<D_93_2>Name here</D_93_2>
</S_N1>
</G_N1>
<G_PO1>
<S_PO1>
<D_350_1>001</D_350_1>
<D_330_2>1</D_330_2>
<D_212_4>344.35</D_212_4>
<D_234_7>10038</D_234_7>
<D_234_9>0135010038</D_234_9>
</S_PO1>
<G_ACK>
<S_ACK>
<D_668_1>AC</D_668_1>
</S_ACK>
</G_ACK>
</G_PO1>
<G_CTT>
<S_CTT>
<D_354_1>1</D_354_1>
</S_CTT>
</G_CTT>
<S_SE>
<D_96_1>6</D_96_1>
<D_329_2>1126</D_329_2>
</S_SE>
</M_855>
Call LoadFrom() var interchange = Interchange.LoadFrom(xml)
Error
InnerException: {"<M_855 xmlns='www.edifabric.com/x12'> was not expected."}
Message: "There is an error in XML document (0, 0)."
Is this how it's supposed to be done? I've also tried creating an new Interchange() object and attaching the XML as Message.Item but receive the same error message.
So after much frustration, I got it working. The structure of the XML in my original post was incomplete. The M_855 node above was only part of the XML I needed to pass into the Interchange.LoadFrom() method.
The correct structure needs to be a full X12 envelope, starting with an INTERCHANGE node.
<INTERCHANGE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="www.edifabric.com/x12">
<S_ISA>
...
</S_ISA>
<GROUPS>
<GROUP>
<S_GS>
...
</S_GS>
<MESSAGES>
<MESSAGE>
<Item>
<M_855>
...
</M_855>
</Item>
<Context>
<Tag>855</Tag>
<Version>004010</Version>
<Format>X12</Format>
</Context>
</MESSAGE>
</MESSAGES>
<S_GE>
...
</S_GE>
</GROUP>
</GROUPS>
<S_IEA>
...
</S_IEA>
</INTERCHANGE>
So now instead of serializing the M_855 object, I am doing this:
var interchange855 = new Interchange();
interchange855.Isa = new S_ISA()
{
...
}
interchange855.Groups.Add(new Group());
interchange855.Iea = new S_IEA()
{
...
}
var edi = interchange855.ToEdi(); //works!