views:

376

answers:

2

The following xml file always seems to validate. Not sure why, but when I remove the following ' xmlns="urn:schemas-microsoft-com:office:spreadsheet" ', it seems to throw a validation error as expected.

Somehow MS is preventing validation with the added XSD office schema. So anytime that XSD Schema is included you can't validate XML using the .NET framework.

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"&gt;
 <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"&gt;
  <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>
A: 

You refer to that namespace here:

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

with x:FullColumns and x:FullRows

David Walker
How does this answers my question?
eschneider
You said:xmlns="urn:schemas-microsoft-com:office:spreadsheet" ', it seems to throw a validation error as expected.It throws that error because that namespace is referenced in the XML.
David Walker
A: 

Your xsd is having no effect. The xsd for urn:schemas-microsoft-com:office:spreadsheet is being picked up from elsewhere, probably built into the .net framework, and being used even though you haven't explicitly told the validator where to find it. This is allowed within the rules for schema validation.

Alohci
I am validating with my own XSD schema using code, not the embedded Office schema.
eschneider