Project

Profile

Help

Support #6311

closed

Problem with XSD Validation of a XML-file with Saxon EE in .NET

Added by Michael Eriksen 4 months ago. Updated 4 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Sprint/Milestone:
-
Start date:
2024-01-02
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

Description

Hello,

I hope that it is okay to ask the following question... I have tried a lot based on your sample for version 10 to C#/.NET.

The case is that I have a XML-file with at least one XSD-error in it - according to the XSD-file. But when I try to code my solution - based on your samplecode I can't return the error. It tells me that there are zero errors in the XML-file. (The code is listed at the end of this question) Maybe I have used the code wrong - Is it possible for you to say anything about this situation - how to correct this? Or is the code correct?

Furthermore I only have an evaluation version for the moment - and therefore I get an exception which tells me that I need a license. Does this mean something for reporting the XSD-error(s) - until I purchase your solution?

I have attached the files I have used for my XSD-validation solution.

I really hope that you are willing to say what I am missing or have made wrong in according to the following code so I can report the error - if it not is correctly made.

Thanks in advance.

            Processor processor = new Processor(true);
            processor.SetProperty("http://saxon.sf.net/feature/timing", "true");
            processor.SetProperty("http://saxon.sf.net/feature/validation-warnings", "false"); //Set to true to suppress the exception
            SchemaManager manager = processor.SchemaManager;
            manager.XsdVersion = "1.1";
            List<XmlProcessingError> xmlProcessingErrors = new List<XmlProcessingError>();
            manager.SetErrorList(xmlProcessingErrors);
            Uri schemaUri = new Uri("C:\\UBL-Invoice-2.1.xsd");

            try
            {
                manager.Compile(schemaUri);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine("Schema compilation failed with " + manager.GetErrorList().Count + " errors");
                foreach (StaticError error in manager.GetErrorList())
                {
                    Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
                }
                //return;
            }

            // Use this to validate an instance document

            SchemaValidator validator = manager.NewSchemaValidator();

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Ignore;
            String inputFileName = new Uri("C:\\MCE-Invoice.xml").ToString();
            XmlReader xmlReader = XmlReader.Create(inputFileName, settings);
            validator.SetSource(xmlReader);
            Console.WriteLine("Validating input file " + inputFileName);
            validator.ErrorList = new List<ValidationFailure>();
            XdmDestination psvi = new XdmDestination();
            validator.SetDestination(psvi);

            try
            {
                validator.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine("Instance validation failed with " + validator.ErrorList.Count + " errors");
                foreach (ValidationFailure error in validator.ErrorList)
                {
                    Console.WriteLine("At line " + error.GetLineNumber() + ": " + error.GetMessage());
                }
                return;
            }

            Console.WriteLine("Input file is valid");

Regards Michael


Files

UBL-Invoice-2.1.xsd (59.7 KB) UBL-Invoice-2.1.xsd Michael Eriksen, 2024-01-02 21:03
MCE-Invoice.xml (278 KB) MCE-Invoice.xml Michael Eriksen, 2024-01-02 21:04
MCE-Invoice.xml (278 KB) MCE-Invoice.xml Michael Eriksen, 2024-01-03 10:59
UBL_v2.1(1).zip (448 KB) UBL_v2.1(1).zip Michael Eriksen, 2024-01-03 11:00

Please register to edit this issue

Also available in: Atom PDF