Validating EDI Files II

Index

    Validating EDI Files II

    Validating EDI files is extremely simple. Just set a few properties and you’re ready to go.

    Example

    // Create an instance of the EDIDocument object

    EDIDocument ediDocument = new EDIDocument();

    // Create your EDI Document here, adding loops segments, data elements etc.

    // Create an instance of the EDIValidator object
    EDIValidator validator = new EDIValidator();

    // Set the type of EDI file that you are about the validate
    validator.EDIFileType = FileType.X12; //Or FileType.EDIFACT

    // Set the EDI rules file. The entire EDI Rules can also be passed in using the ‘EDIRuleFileData’ property
    validator.EDIRulesFile = “C:\\EDIFile.Rules”;

    // Validate the EDI Document
    validator.Validate(ediDocument);

    // Check if the EDI file passed
    if (validator.Passed)
    {
        Console.WriteLine(“Passed”);

        // Get the EDI file just parsed
        EDILightWeightDocument file = validator.EDILightWeightDocument;
    }
    else
    {
       // Display all errors

    foreach (EDIError error in ediValidator.Errors)
       {
          Console.WriteLine(error.LineNumber.ToString() + ” ” + error.Loop + ” ” + error.Segment + ” ” + error.Message.ToString());
       }
    }

    You can also validate an EDILightWeightDocument using this method. If you just want to validate the EDI file and not load it just set the LoadValidatedData property to false. In this case EDIDocument property of EDIValidator will be null after validation.

    Prior to RDPCrystal EDI Library 4.0 a ValidationException could have been thrown if an unexpected exception occurred. Currently if an unexpected error occurs an Error object is created and returned in the Errors collection. In this case an EDIValidationMessage type of UnexpectedError or MalformedEDIFile is returned in the Error object. An exception will be thrown if the file to validate is not found or the rules file to use is not found.

    in EDI Validator
    Did this article answer your question?