为什么XSD验证始终适用于此文件?

时间:2009-06-29 21:41:39

标签: .net xml xsd

以下xml文件似乎总是在验证。不知道为什么,但当我删除以下'xmlns =“urn:schemas-microsoft-com:office:spreadsheet”'时,它似乎会按预期抛出验证错误。

MS以某种方式阻止使用添加的XSD office 架构进行验证。因此,只要包含XSD Schema,就无法使用.NET框架验证XML。

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>

3 个答案:

答案 0 :(得分:1)

您的xsd无效。 xsd for urn:schemas-microsoft-com:office:电子表格正在从其他地方获取,可能内置于.net框架中,即使你没有明确地告诉验证器在哪里找到它,也会被使用。这可以在模式验证规则中使用。

答案 1 :(得分:1)

以下是我的案例:

settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings

但要小心,它可能引发通常被忽略的其他错误。

REF: http://www.dotnetspider.com/resources/2971-Validating-an-XML-File-using-XSD-NET.aspx

答案 2 :(得分:0)

您在此处引用该命名空间:

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

使用x:FullColumns和x:FullRows