2009-06-29 3 views
1

Le fichier xml suivant semble toujours valider. Je ne sais pas pourquoi, mais quand je supprime le suivant 'xmlns = "urn: schémas-microsoft-com: bureau: tableur"', il semble lancer une erreur de validation comme prévu. D'une manière ou d'une autre, MS empêche la validation avec le schéma XSD office ajouté. Ainsi, chaque fois que XSD Schema est inclus, vous ne pouvez pas valider XML en utilisant le framework .NET.Pourquoi la validation XSD fonctionne-t-elle toujours pour ce fichier?

Option Strict On 
Option Explicit On 

Imports System.IO 
Imports System.Xml 
Imports System.Xml.Schema 
Imports System.Text 

Public Class XMLValidator2 

    Private _isValid As Boolean = False 

    'Validation Error Count 
    Private _ErrorsCount As Integer = 0 

    'Validation Error Message 
    Private _ErrorMessage As String = "" 

    'Declare local objects 
    Private _tr As XmlTextReader 
    Private _xr As XmlTextReader 
    Private _xsc As XmlSchemaSet 
    Private _vr As XmlReader 

    Public Sub Validate(ByVal strXMLDocPath As String, ByVal strXSDPath As String) 

     Try 

      _isValid = False 
      'Text reader object 
      _tr = New XmlTextReader(strXSDPath) 
      _xsc = New XmlSchemaSet 
      _xsc.Add("", _tr) 

      'Validator Object 
      Dim settings As New XmlReaderSettings() 
      settings.Schemas.Add(_xsc) 
      settings.ValidationType = ValidationType.Schema 
      'Add validation event handler 
      AddHandler settings.ValidationEventHandler, AddressOf Me.ValidationHandler 
      _vr = XmlReader.Create(strXMLDocPath, settings) 

      _ErrorsCount = 0 
      _ErrorMessage = "" 

      'Validate XML data 
      While (_vr.Read()) 
      End While 
      _vr.Close() 
      ' Raise exception, if XML validation fails 
      If (_ErrorsCount > 0) Then Throw New Exception(_ErrorMessage) 
      'XML Validation succeeded 
      _isValid = True 

     Catch ex As System.Exception 
      'XML Validation failed 
      Throw New Exception(ex.Message) 
     End Try 
    End Sub 

    Private Sub ValidationHandler(ByVal sender As Object, ByVal args As ValidationEventArgs) 
     _ErrorMessage = _ErrorMessage + args.Message + "\r\n" 
     _ErrorsCount = +1 
    End Sub 

    Public ReadOnly Property bIsValid() As Boolean 
     Get 
      Return _isValid 
     End Get 
    End Property 
End Class 

<?xml version="1.0"?> 
<?mso-application progid="Excel.Sheet"?> 
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"> 
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> 
    <LastAuthor>KLIMMPI</LastAuthor> 
    <Created>2009-06-04T13:49:21Z</Created> 
    <LastSaved>2009-06-04T16:10:37Z</LastSaved> 
    <Version>11.9999</Version> 
</DocumentProperties> 
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> 
    <Colors> 
    <Color> 
    <Index>17</Index> 
    <RGB>#663399</RGB> 
    </Color> 
    <Color> 
    <Index>39</Index> 
    <RGB>#E3E3E3</RGB> 
    </Color> 
    <Color> 
    <Index>45</Index> 
    <RGB>#FF3300</RGB> 
    </Color> 
    </Colors> 
</OfficeDocumentSettings> 
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> 
    <WindowHeight>8790</WindowHeight> 
    <WindowWidth>30840</WindowWidth> 
    <WindowTopX>480</WindowTopX> 
    <WindowTopY>90</WindowTopY> 
    <ProtectStructure>False</ProtectStructure> 
    <ProtectWindows>False</ProtectWindows> 
</ExcelWorkbook> 
<Styles> 
    <Style ss:ID="Default" ss:Name="Normal"> 
    <Alignment ss:Vertical="Bottom"/> 
    <Borders/> 
    <Font/> 
    <Interior/> 
    <NumberFormat/> 
    <Protection/> 
    </Style> 
    <Style ss:ID="s22" ss:Name="Normal_Not 8 Counts"> 
    <Alignment ss:Vertical="Bottom"/> 
    <Borders/> 
    <Font ss:FontName="Arial "/> 
    <Interior/> 
    <NumberFormat/> 
    <Protection/> 
    </Style> 
    <Style ss:ID="s24"> 
    <Font ss:Bold="1"/> 
    </Style> 
    <Style ss:ID="s25" ss:Parent="s22"> 
    <Alignment ss:Horizontal="Right" ss:Vertical="Center"/> 
    <Borders/> 
    <Font ss:FontName="Arial "/> 
    <Interior/> 
    <NumberFormat/> 
    </Style> 
    <Style ss:ID="s26" ss:Parent="s22"> 
    <Alignment ss:Horizontal="Left" ss:Vertical="Center"/> 
    <Borders/> 
    <Font ss:FontName="Arial "/> 
    <Interior/> 
    <NumberFormat/> 
    </Style> 
    <Style ss:ID="s27"> 
    <Borders/> 
    <Font ss:Bold="1"/> 
    <Interior/> 
    </Style> 
    <Style ss:ID="s28"> 
    <Borders/> 
    <Interior/> 
    </Style> 
    <Style ss:ID="s29"> 
    <Alignment ss:Horizontal="Right" ss:Vertical="Bottom"/> 
    <Borders/> 
    <Interior/> 
    <NumberFormat ss:Format="@"/> 
    </Style> 
</Styles> 
<Worksheet ss:Name="Not 8 Counts"> 
    <Table ss:ExpandedColumnCount="8" ss:ExpandedRowCount="8" x:FullColumns="1" 
    x:FullRows="1"> 
    <Column ss:AutoFitWidth="0" ss:Width="101.25"/> 
    <Column ss:StyleID="s28" ss:AutoFitWidth="0" ss:Width="66.75"/> 
    <Column ss:StyleID="s28" ss:AutoFitWidth="0" ss:Width="52.5"/> 
    <Column ss:StyleID="s28" ss:AutoFitWidth="0" ss:Width="117.75"/> 
    <Column ss:AutoFitWidth="0" ss:Width="62.25"/> 
    <Column ss:Index="7" ss:AutoFitWidth="0" ss:Width="68.25"/> 
    <Row> 
    <Cell ss:StyleID="s24"><Data ss:Type="String">SPAccountIdentifier</Data></Cell> 
    <Cell ss:StyleID="s27"><Data ss:Type="String">SPUIAccount</Data></Cell> 
    <Cell ss:StyleID="s27"><Data ss:Type="String">SPFein</Data></Cell> 
    <Cell ss:StyleID="s27"><Data ss:Type="String">SPLegalName</Data></Cell> 
    <Cell ss:StyleID="s24"><Data ss:Type="String">SPRateYear</Data></Cell> 
    <Cell ss:Index="7" ss:StyleID="s24"><Data ss:Type="String">Notes</Data></Cell> 
    </Row> 
    <Row> 
    <Cell><Data ss:Type="Number">1</Data></Cell> 
    <Cell><Data ss:Type="Number">204006</Data></Cell> 
    <Cell ss:StyleID="s26"><Data ss:Type="String">397221</Data></Cell> 
    <Cell ss:StyleID="s26"><Data ss:Type="String">Fake</Data></Cell> 
    <Cell><Data ss:Type="Number">2009</Data></Cell> 
    <Cell ss:Index="7"><Data ss:Type="String">open subject</Data></Cell> 
    <Cell><Data ss:Type="String">Account number dropped proceeding zeros</Data></Cell> 
    </Row> 
    <Row> 
    <Cell><Data ss:Type="Number">2</Data></Cell> 
    <Cell ss:StyleID="s29"><Data ss:Type="String">0018008</Data></Cell> 
    <Cell ss:StyleID="s26"><Data ss:Type="String">3905530</Data></Cell> 
    <Cell ss:StyleID="s26"><Data ss:Type="String">Fake</Data></Cell> 
    <Cell><Data ss:Type="Number">2009</Data></Cell> 
    <Cell ss:Index="8"><Data ss:Type="String">account number should be 001008</Data></Cell> 
    </Row> 
    <Row> 
    <Cell><Data ss:Type="Number">6</Data></Cell> 
    <Cell ss:StyleID="s25"><Data ss:Type="String">04045002</Data></Cell> 
    <Cell ss:StyleID="s26"><Data ss:Type="String">3915659</Data></Cell> 
    <Cell ss:StyleID="s26"><Data ss:Type="String">FAKE</Data></Cell> 
    <Cell><Data ss:Type="Number">2009</Data></Cell> 
    </Row> 
    <Row> 
    <Cell ss:Index="2" ss:StyleID="s29"/> 
    </Row> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <Print> 
    <ValidPrinterInfo/> 
    <HorizontalResolution>600</HorizontalResolution> 
    <VerticalResolution>600</VerticalResolution> 
    <NumberofCopies>0</NumberofCopies> 
    </Print> 
    <Selected/> 
    <Panes> 
    <Pane> 
    <Number>3</Number> 
    <ActiveRow>13</ActiveRow> 
    <ActiveCol>3</ActiveCol> 
    </Pane> 
    </Panes> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    </WorksheetOptions> 
</Worksheet> 
</Workbook> 

XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="Address"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Recipient" type="xs:string" /> 
     <xs:element name="House" type="xs:string" /> 
     <xs:element name="Street" type="xs:string" /> 
     <xs:element name="Town" type="xs:string" /> 
     <xs:element name="County" type="xs:string" minOccurs="0" /> 
     <xs:element name="PostCode" type="xs:string" /> 
     <xs:element name="Country"> 
      <xs:simpleType> 
      <xs:restriction base="xs:string"> 
       <xs:enumeration value="FR" /> 
       <xs:enumeration value="DE" /> 
       <xs:enumeration value="ES" /> 
       <xs:enumeration value="UK" /> 
       <xs:enumeration value="US" /> 
      </xs:restriction> 
      </xs:simpleType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
+0

pas des erreurs surviennent – eschneider

+0

Quelle erreur de validation, il ne jette, et quel logiciel utilisez-vous pour effectuer la validation? – skaffman

+0

vous avez dit "il semble lancer une erreur de validation comme prévu" – skaffman

Répondre

0

Vous faites référence à cet espace de noms ici:

<Worksheet ss:Name="Not 8 Counts"> 
    <Table ss:ExpandedColumnCount="8" ss:ExpandedRowCount="8" x:FullColumns="1" x:FullRows="1"> 

avec x: x: FullColumns et FullRows

+0

Comment cela répond-il à ma question? – eschneider

+0

Vous avez dit: xmlns = "urn: schémas-microsoft-com: bureau: tableur" ', il semble lancer une erreur de validation comme prévu. Il lève cette erreur car cet espace de noms est référencé dans le fichier XML. –

1

Votre xsd est sans effet. La feuille de calcul xsd pour urn: schemas-microsoft-com: office: est extraite d'ailleurs, probablement intégrée au framework .net, et utilisée même si vous n'avez pas explicitement indiqué au validateur où la trouver. Ceci est autorisé dans les règles de validation du schéma.

+0

Je valide avec mon propre schéma XSD en utilisant du code, pas le schéma Office intégré. – eschneider

+0

Savez-vous comment éviter ce comportement? – jlp

+0

@jlp - Ce n'est pas une zone de .net que je connais assez bien, mais il existe généralement un moyen de fournir votre propre chargeur de ressources/résolveur pour remplacer celui qui est par défaut. Lorsque vous chargez des xsds et des DTD à partir d'URL distantes, ces serveurs limitent souvent le nombre de requêtes d'un même client. La plupart des langages fournissent donc un mécanisme permettant de surcharger le résolveur de ressources pour accéder à ces fichiers depuis un magasin local. – Alohci

Questions connexes