Search code examples
xmlxsdxsd-validationxml-validation

XML schema not found even if specified in file


I'm trying something as simple as generating XML that respects the rules of an XML Schema. However, I have not myself specified the XSD file and have just been given an example of how the header of my XML should be, but for some reason the schema can not be found automatically by e.g. Notepad++ or by XmlReaderSettings in my C# project.

Here is a example of my XML:

<?xml version="1.0" encoding="utf-8"?>
<PBSXML000:FullDelivery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://pbs-erhverv.dk/online/xml/bs/pbsxml000bigmax http://pbs-erhverv.dk/online/xml/bs/pbsxml000bigmax.xsd" xmlns:PBSXML000="http://pbs-erhverv.dk/online/xml/bs/pbsxml000bigmax">
  <PBSXML000:DeliveryStart>
    <PBSXML000:DataSupplierCVR>00000</PBSXML000:DataSupplierCVR>
    <PBSXML000:DataSupplierSysCode>000</PBSXML000:DataSupplierSysCode>
    <PBSXML000:DeliveryId>12345678</PBSXML000:DeliveryId>
    <PBSXML000:DeliveryCreateDate>211119</PBSXML000:DeliveryCreateDate>
  </PBSXML000:DeliveryStart>
  <PBSXML000:SectionStart>
    <PBSXML000:CreditorPBSNr>12345678</PBSXML000:CreditorPBSNr>
    <PBSXML000:SectionNr>0112</PBSXML000:SectionNr>
    <PBSXML000:DebtorGroupNr>1</PBSXML000:DebtorGroupNr>
  </PBSXML000:SectionStart>
  <PBSXML000:DebtorIdentification>
    <PBSXML000:DebtorCustomerNr>99100045</PBSXML000:DebtorCustomerNr>
    <PBSXML000:AgreementNr>999100012</PBSXML000:AgreementNr>
    <PBSXML000:DuePayDate>01122019</PBSXML000:DuePayDate>
    <PBSXML000:DebtorCVROrCPRNr>12345678</PBSXML000:DebtorCVROrCPRNr>
  </PBSXML000:DebtorIdentification>
  <PBSXML000:M601Record>
    <PBSXML000:RecordType022>
      <PBSXML000:DebtorNameAddr1>Hans Ole, blalba 17, st th</PBSXML000:DebtorNameAddr1>
      <PBSXML000:DebtorPostCode>2100</PBSXML000:DebtorPostCode>
      <PBSXML000:DebtorCountryCode>DK</PBSXML000:DebtorCountryCode>
    </PBSXML000:RecordType022>
    <PBSXML000:RecordType042>
      <PBSXML000:SignCode>1</PBSXML000:SignCode>
      <PBSXML000:Amount>100095</PBSXML000:Amount>
    </PBSXML000:RecordType042>
    <PBSXML000:RecordType052>
      <PBSXML000:TextNoteRecordNr>11335</PBSXML000:TextNoteRecordNr>
      <PBSXML000:TextLine>Ost - den gode</PBSXML000:TextLine>
    </PBSXML000:RecordType052>
  </PBSXML000:M601Record>
  <PBSXML000:DebtorIdentification>
    <PBSXML000:DebtorCustomerNr>12345</PBSXML000:DebtorCustomerNr>
    <PBSXML000:AgreementNr>12345</PBSXML000:AgreementNr>
    <PBSXML000:DuePayDate>01122019</PBSXML000:DuePayDate>
    <PBSXML000:DebtorCVROrCPRNr>12345678</PBSXML000:DebtorCVROrCPRNr>
  </PBSXML000:DebtorIdentification>
  <PBSXML000:SectionEnd>
    <PBSXML000:Amountin601>1334690</PBSXML000:Amountin601>
  </PBSXML000:SectionEnd>
  <PBSXML000:DeliveryEnd>
    <PBSXML000:NrOfAll042in601>54</PBSXML000:NrOfAll042in601>
    <PBSXML000:TotalAmountin601>0</PBSXML000:TotalAmountin601>
    <PBSXML000:NrOfAll052in601>32</PBSXML000:NrOfAll052in601>
    <PBSXML000:NrOfAll022in601>22</PBSXML000:NrOfAll022in601>
  </PBSXML000:DeliveryEnd>
</PBSXML000:FullDelivery>

And the header of the schema file is this:

<xs:schema xmlns:PBSXML000="http://pbs-erhverv.dk/online/xml/bs/pbsxml000bigmax" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://pbs-erhverv.dk/online/xml/bs/pbsxml000bigmax" elementFormDefault="qualified" attributeFormDefault="unqualified">

When I try to test is just get the error:

Failed to locate the main schema resource at

'http://pbs-erhverv.dk/online/xml/bs/pbsxml000bigmax.xsd'

Do you have any idea why this simple thing fails?


Solution

  • Okay, I found a workaround, I could just specific add the schema from the url to the xsd, it's not perfect but I can live with it :) If someone has a similar problem I just used:

    XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.ValidationType = ValidationType.Schema;
            readerSettings.Schemas.Add("http://pbserhverv.dk/online/xml/bs/pbsxml000bigmax",
                "http://pbs-erhverv.dk/online/xml/bs/pbsxml000bigmax.xsd");
    

    As to just make it fetch the xsd.