XML 即使在文件中指定,也找不到架构

XML schema not found even if specified in file

我正在尝试生成符合 XML 模式规则的 XML 之类的简单方法。但是,我自己并没有指定 XSD 文件,只是给出了一个示例,说明我的 XML 的 header 应该如何,但由于某种原因无法自动找到架构通过例如Notepad++XmlReaderSettings 在我的 C# 项目中。

这是我的例子 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>

架构文件的header是这样的:

<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">

当我尝试测试时只是得到错误:

无法在

找到主架构资源

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

你知道为什么这个简单的事情会失败吗?

您的 XML 通过 xsi:schemaLocation 正确指定了关联的 XSD。我能够验证它并确定:

  1. 警告:大 maxOccurs 值被解释为 unbounded
  2. 错误PBSXML000:DataSupplierCVR 必须有 8 位数字。

如果您收到有关无法找到指定 XSD 的错误,请尝试通过网络浏览器加载 XSD 以检查连接。

好的,我找到了一个解决方法,我可以将 url 中的架构具体添加到 xsd,它并不完美,但我可以接受它:) 如果有人有类似的问题我刚刚使用:

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");

至于让它获取 xsd。