Using EDIFabric to Determine File Type
I am trying to devise a strategy whereby we author an Azure Function to monitor an SFTP site for EDI files delivered to us from a clearinghouse. In this SFTP folder we may receive a TA1, 835, etc. We want to check the folder periodically, and then get a list of the files in the folder. We would then grab a file and process it based on its type.
What is the best way to determine the type of file we received? Once we know the type, we can pass it to a .NET function designed to parse that type of EDI file into our data model.
Many thanks...
Comments
3 comments
Hi,
I create empty lists of edi messages ( = new List<EdiMessage>() ) matching the file types I am possibly expecting (one list per type). open the files one at time using the X12Reader, read the file to a list of EdiItems using StreamReader, then for each EdiItem in the list I check if it contains the type I am looking for, if so, add it to the appropriate EDI message list to process after all files have been processed from the SFTP server.
There are more elegant ways to perform this process but this has worked fine for me.
Stream ediStream = new StreamReader(filepath).BaseStream;
List<EdiItem> ediItems;
List<EdiMessage> edi944s = new List<EdiMessage>();
List<EdiMessage> edi945s = new List<EdiMessage>();
List<EdiMessage> edi997s = new List<EdiMessage>();
List<EdiMessage> edi947s = new List<EdiMessage>();
List<EdiMessage> edi846s = new List<EdiMessage>();
try {
using (var ediReader = new X12Reader(ediStream, "EDI Fabric", Encoding.ASCII, true))
{
ediItems = ediReader.ReadToEnd().ToList();
foreach (EdiItem ediItem in ediItems)
{
if (ediItem.ToString().Contains("944"))
{
edi944s.Add((EdiMessage)ediItem);
}
if (ediItem.ToString().Contains("945"))
{
edi945s.Add((EdiMessage)ediItem);
}
if (ediItem.ToString().Contains("997"))
{
edi997s.Add((EdiMessage)ediItem);
}
if (ediItem.ToString().Contains("947"))
{
edi947s.Add((EdiMessage)ediItem);
}
if (ediItem.ToString().Contains("846"))
{
edi846s.Add((EdiMessage)ediItem);
}
}
//Process 947s
foreach (EdiMessage ediMess947 in edi947s)
{...}
Thanks you very much, Robert. This looks like a good approach and I'll try to create a working example in my project with it.
Hello,
Here is how you resolve the message type before loading the appropriate template. Here is the GitHub example.
Another example of how to determine the message type is by checking the type - example in GitHub.
So, in the snippet above you won't do (because you'll be doing a second foreach, and no need to do ToString):
but do this:
Please sign in to leave a comment.