0

I'm running into an Exception error when trying to validate an XML file against a schema in MVC/ASP.Net Core.

The error I'm getting is this:

System.Xml.Schema.XmlSchemaValidationException: 'Type 'http://ns.editeur.org/onix/3.0/reference:SourceTypeCode' is not declared, or is not a simple type.'

My code is this:

XmlSchemaSet onixschema = new XmlSchemaSet();

XDocument xmlDocument = XDocument.Load(@"path.to.xml.file");

onixschema.Add("http://ns.editeur.org/onix/3.0/reference", @"path.to.ONIX_BookProduct_3.0_reference.xsd");

xmlDocument.Validate(onixschema, (o, e) =>
        {
            validationResult.ErrorMessages.Add(e.Message);
        });

I don't really know where to start with the exception. The message isn't wildly enlightening!

And the really baffling thing is that if I run this exact code in WebForms/.Net (same files, etc), it validates correctly.

I don't get the error.

If anybody was able to shed some light on this, I'd be very grateful.

//Edit

Pretty sure it has something to do with ASP.Net Core. If I create a brand new MVC/.Net web app, and stick the code in the Home Controller, it works as it should. No validation exception.

If I create a brand new MVC/ASP.NET Core App, and stick the code in the Home Controller, it fails, with the validation exception.

Could it be to do with the way ASP.NET Core handles the static XSD file?

RichardJ
  • 95
  • 1
  • 2
  • 9
  • XmlSchemaSet Add() is defined as follows : public XmlSchema Add(string targetNamespace, string schemaUri); Your add doesn't have a namespace as first parameter. – jdweng Jun 13 '18 at 04:06
  • Isn't the targetNamespace "http://ns.editeur.org/onix/3.0/reference"? Unless I'm missing something my add does have 2 parameters (see the code)... – RichardJ Jun 13 '18 at 09:59
  • Two string, but the wrong strings. You first parameter is an URI which should be the second parameter. You do not have a namespace. – jdweng Jun 13 '18 at 10:44
  • Hi - Thanks, but the namespace for the ONIX specification is a URI. From the official documentation: – RichardJ Jun 13 '18 at 11:12
  • No. The names space ABC would normally be xmlns:ABC=="ns.editeur.org/onix/3.0/reference". When namespace is omitted it means it is the default namespace. So in your case the namespace is xsi. – jdweng Jun 13 '18 at 14:33
  • Thanks. The specification document says: "The new namespace is ‘http://ns.editeur.org/onix/3.0/reference’ with the ‘ns’ serving to highlight the fact that this is a namespace only, and should not be expected to resolve." If I change the namespace to anything other than the original, I get: 'The targetNamespace parameter '[whatever I use]' should be the same value as the targetNamespace 'http://ns.editeur.org/onix/3.0/reference' of the schema.' If I use exactly this code with original namespace, & exactly same files, it validates correctly in a blank MVC/.NET or Web Forms project. – RichardJ Jun 13 '18 at 14:56
  • I checked the sample code at the webpage and there is no namespace. So I would use the URI "http://ns.editeur.org/onix/3.0/reference" and use null for the namespace. – jdweng Jun 13 '18 at 18:07
  • Thanks. Unfortunately, using null just gives me exactly the same error. Except now it just says "Type 'SourceTypeCode' is not declared". Ah well! Thanks for all your help. – RichardJ Jun 13 '18 at 18:27
  • Edit: Ah, 'SourceTypeCode' is the name of an attribute group defined in the XSD file. Making progress! I think it's not picking up on the files linked in the schema even though they're in the same directory: – RichardJ Jun 13 '18 at 18:44
  • Do you have 3.0, 3.0.1, 3.0.2, 3.0.3, 3.0.4? See : http://www.editeur.org/12/About-Release-3.0. See page 10 :http://www.editeur.org/files/ONIX%203/Changes%20for%20ONIX%203.0.1.pdf, You may need to add the xmlns:xsi="http://www.w3.org/2001/XMLSchema`instance" to your schema set. – jdweng Jun 13 '18 at 21:43

2 Answers2

1

Okay, I've sort of answered the question.

The problem was that the XSD file imports two further XSD files. If I import these manually creating a much larger XSD file, then the problem goes away.

So it's got something to do with the way ASP.NET Core handles relative paths. It's not locating recognizing that there are two XSD files to be imported, or it can't find them (whereas Web Forms & MVC/.NET is able to do so). I don't know why yet!

RichardJ
  • 95
  • 1
  • 2
  • 9
-1

The message is clear : SourceTypeCode' is not declared.

To be sure that your xml file doesn't have any error, you can valid it using XmlTools on notepad ++.

if you don't have errors, then we can be sure that the problem is in your code and not in the file.

Plugins => Xml Tools

Regards,

  • Thanks, but it works perfectly well in an MVC/.Net application, and in a Web Forms application. Same file, same code. – RichardJ Jun 13 '18 at 12:26
  • I think your xml actually has a validation error with SourceTypeCode. The schema on following webpage is not very helpful but the sample xml may help you : http://www.editeur.org/93/Release-3.0-Downloads/ – jdweng Jun 13 '18 at 18:39
  • This is sort of what happened. The schema is referencing types that are defined in two imported XSD files. They weren't being imported - I don't know why - so you get the type not defined error. If you combine the XSD files together - in effect doing a manual import - the error goes away, because the simple type is now defined in the XSD file. Thanks for all your time and help. Appreciated! – RichardJ Jun 13 '18 at 21:20