Skip to content

Instantly share code, notes, and snippets.

@kiquenet
Last active December 7, 2018 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kiquenet/86090779650975b7426a7f798174eef6 to your computer and use it in GitHub Desktop.
Save kiquenet/86090779650975b7426a7f798174eef6 to your computer and use it in GitHub Desktop.
XML Validation using XSD
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns="http://www.seg-social.es/creta/esquemas/V100/DCL" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.seg-social.es/creta/esquemas/V100/DCL" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="DCL">
<xs:complexType>
<xs:sequence>
<xs:element ref="Autorizado"/>
<xs:element ref="Liquidacion"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Autorizado">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d\d\d\d\d\d\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Liquidacion">
<xs:complexType>
<xs:sequence>
<xs:element ref="Ccc"/>
<xs:element ref="CccConcertado" minOccurs="0"/>
<xs:element ref="PeriodoDesde"/>
<xs:element ref="PeriodoHasta"/>
<xs:element ref="Tipo"/>
<xs:element ref="FechaControl" minOccurs="0"/>
<xs:element ref="FechaHoraRecaudacion"/>
<xs:element ref="TipoDCL"/>
<xs:element ref="DatosCabeceraDCL"/>
<xs:element ref="DatosCuerpoDCL"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="CtaCot">
<xs:sequence>
<xs:element ref="Regimen"/>
<xs:element ref="Provincia"/>
<xs:element ref="Numero"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Ccc" type="CtaCot"/>
<xs:element name="CccConcertado" type="CtaCot"/>
<xs:element name="Regimen">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d\d\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Provincia">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Numero">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d{9}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:complexType name="Periodo">
<xs:sequence>
<xs:element ref="Mes"/>
<xs:element ref="Anho"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Mes">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="12"/>
<xs:enumeration value="11"/>
<xs:enumeration value="10"/>
<xs:enumeration value="09"/>
<xs:enumeration value="08"/>
<xs:enumeration value="07"/>
<xs:enumeration value="06"/>
<xs:enumeration value="05"/>
<xs:enumeration value="04"/>
<xs:enumeration value="03"/>
<xs:enumeration value="02"/>
<xs:enumeration value="01"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Anho">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="4"/>
<xs:pattern value="\d{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="PeriodoDesde" type="Periodo"/>
<xs:element name="PeriodoHasta" type="Periodo"/>
<xs:element name="Tipo">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="3"/>
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="FechaControl" type="Periodo"/>
<xs:element name="FechaHoraRecaudacion">
<xs:complexType>
<xs:sequence>
<xs:element ref="FechaRecaudacion"/>
<xs:element ref="HoraRecaudacion"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Fecha">
<xs:sequence>
<xs:element ref="Dia"/>
<xs:element ref="Mes"/>
<xs:element ref="Anho"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Dia">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="2"/>
<xs:pattern value="\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="FechaRecaudacion" type="Fecha"/>
<xs:element name="HoraRecaudacion">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="6"/>
<xs:pattern value="\d{6}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="TipoDCL">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="DatosCabeceraDCL">
<xs:complexType>
<xs:sequence>
<xs:element ref="AlcanceCalculo"/>
<xs:element ref="CuotaALiquidar"/>
<xs:element ref="DatosEnvio"/>
<xs:element ref="DatosLiquidacion"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="AlcanceCalculo">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="CuotaALiquidar" type="xs:string"/>
<xs:element name="DatosEnvio">
<xs:complexType>
<xs:sequence>
<xs:element ref="Autorizado"/>
<xs:element ref="Envio"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Envio">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="10"/>
<xs:pattern value="\d{10}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="DatosLiquidacion">
<xs:complexType>
<xs:sequence>
<xs:element ref="Ccc"/>
<xs:element ref="PeriodoDesde"/>
<xs:element ref="PeriodoHasta"/>
<xs:element ref="DescripcionTipoLiquidacion"/>
<xs:element ref="RazonSocial"/>
<xs:element ref="Empresario"/>
<xs:element ref="EntidadAtEp"/>
<xs:element ref="NumeroTrabajadores"/>
<xs:element ref="NumeroLiquidacion"/>
<xs:element ref="MesIngresoCuotasDiferidas" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DescripcionTipoLiquidacion">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="RazonSocial">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="55"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Empresario" type="Ipf"/>
<xs:complexType name="Ipf">
<xs:sequence>
<xs:element ref="TipoIpf"/>
<xs:element ref="NumeroIpf"/>
</xs:sequence>
</xs:complexType>
<xs:element name="TipoIpf">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\w"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NumeroIpf">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\w{10}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="EntidadAtEp">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NumeroTrabajadores">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="6"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NumeroLiquidacion">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="17"/>
<xs:pattern value="\d{17}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="MesIngresoCuotasDiferidas" type="Periodo"/>
<xs:element name="DatosCuerpoDCL">
<xs:complexType>
<xs:sequence>
<xs:element ref="LineasDCL"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="LineasDCL">
<xs:complexType>
<xs:sequence>
<xs:element ref="LineaDCL" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="LineaDCL">
<xs:complexType>
<xs:sequence>
<xs:element ref="DescripcionLDCL"/>
<xs:element ref="BaseLDCL" minOccurs="0"/>
<xs:element ref="ImporteLDCL"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DescripcionLDCL">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BaseLDCL">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="11"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ImporteLDCL">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="11"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="ISO-8859-1"?>
<ReciboLiquidacionCotizaciones xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.seg-social.es/creta/esquemas/V100/ReciboLiquidacionCotizaciones" xsi:schemaLocation="http://www.seg-social.es/creta/esquemas/V100/ReciboLiquidacionCotizaciones RLC.xsd">
<Autorizado>12345678</Autorizado>
<Liquidacion>
<Ccc>
<Regimen>0111</Regimen>
<Provincia>28</Provincia>
<Numero>170951274</Numero>
</Ccc>
<PeriodoDesde>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoDesde>
<PeriodoHasta>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoHasta>
<Tipo>L00</Tipo>
<FechaHoraRecaudacion>
<FechaRecaudacion>
<Dia>20</Dia>
<Mes>05</Mes>
<Anho>2013</Anho>
</FechaRecaudacion>
<HoraRecaudacion>152514</HoraRecaudacion>
</FechaHoraRecaudacion>
<TipoRLC>T</TipoRLC>
<DatosCabeceraRLC>
<AlcanceCalculo>Total</AlcanceCalculo>
<CuotaALiquidar>Total</CuotaALiquidar>
<DatosEnvio>
<Autorizado>12345678</Autorizado>
<Envio>1335123456</Envio>
</DatosEnvio>
<DatosLiquidacion>
<Ccc>
<Regimen>0111</Regimen>
<Provincia>28</Provincia>
<Numero>170951274</Numero>
</Ccc>
<PeriodoDesde>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoDesde>
<PeriodoHasta>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoHasta>
<DescripcionTipoLiquidacion>L00 - Liquidacion normal</DescripcionTipoLiquidacion>
<RazonSocial>GALERIA COMERCIAL EL CORTE ALCALAINO</RazonSocial>
<Empresario>
<TipoIpf>9</TipoIpf>
<NumeroIpf>0B34987321</NumeroIpf>
</Empresario>
<EntidadAtEp>099 MUTUA DE ACCIDENTES DE TRABAJO</EntidadAtEp>
<NumeroTrabajadores>8</NumeroTrabajadores>
<ModalidadPago>Pago electronico</ModalidadPago>
<NumeroLiquidacion>28201312345678912</NumeroLiquidacion>
</DatosLiquidacion>
<CodificacionesInformaticas>
<ReferenciaCI>RDT12345678901</ReferenciaCI>
<FechaCI>
<Dia>05</Dia>
<Mes>05</Mes>
<Anho>2013</Anho>
</FechaCI>
<HoraCI>123320</HoraCI>
<HuellaCI>POD2Y3T3</HuellaCI>
</CodificacionesInformaticas>
<CabeceraPagoElectronico>
<PeriodoPagoPE>
<PeriodoDesde>
<Mes>05</Mes>
<Anho>2013</Anho>
</PeriodoDesde>
<PeriodoHasta>
<Mes>05</Mes>
<Anho>2013</Anho>
</PeriodoHasta>
</PeriodoPagoPE>
<EmisoraPE>02827003001</EmisoraPE>
<NumeroReferenciaPE>0000170951288</NumeroReferenciaPE>
<IdentificacionPE>040001</IdentificacionPE>
<ImportePE>244541</ImportePE>
</CabeceraPagoElectronico>
</DatosCabeceraRLC>
<DatosCuerpoRLC>
<LineaRLC>
<DescripcionLRLC>CONTINGENCIAS COMUNES</DescripcionLRLC>
<BaseLRLC>1160448</BaseLRLC>
<ImporteLRLC>328407</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>REDUCCIONES A CARGO DE LA TGSS</DescripcionLRLC>
<ImporteLRLC>196137</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>LIQUIDO CONTINGENCIAS COMUNES</DescripcionLRLC>
<ImporteLRLC>132270</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>IT DE ACCIDENTES DE TRABAJO</DescripcionLRLC>
<BaseLRLC>1160448</BaseLRLC>
<ImporteLRLC>12011</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>IMS DE ACCIDENTES DE TRABAJO</DescripcionLRLC>
<BaseLRLC>1160448</BaseLRLC>
<ImporteLRLC>8005</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>LIQUIDO DE ACCIDENTES DE TRABAJO</DescripcionLRLC>
<ImporteLRLC>20016</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>CUOTA DE OTRAS COTIZACIONES</DescripcionLRLC>
<BaseLRLC>1160448</BaseLRLC>
<ImporteLRLC>92255</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>LIQUIDO DE OTRAS COTIZACIONES</DescripcionLRLC>
<ImporteLRLC>92255</ImporteLRLC>
</LineaRLC>
<LineaRLC>
<DescripcionLRLC>LIQUIDO DE TOTALES</DescripcionLRLC>
<ImporteLRLC>244541</ImporteLRLC>
</LineaRLC>
</DatosCuerpoRLC>
<DatosPiePaginaRLC>
<TextoPieRLC>Este recibo no implica el pago de las cuotas si no va acompa�ado del correspondiente comprobante de ingreso, sello o validaci�n mec�nica de la Entidad financiera. Este documento recoge los c�lculos realizados a la fecha de la confirmaci�n/cierre de la liquidaci�n. </TextoPieRLC>
<CodigoBarras>
<IdentificacionAplicacionCB>90</IdentificacionAplicacionCB>
<TipoFormatoCB>581</TipoFormatoCB>
<EmisoraCB>02827003</EmisoraCB>
<SufijoCB>001</SufijoCB>
<ReferenciaCB>0000170951288</ReferenciaCB>
<IdentificadorCB>040001</IdentificadorCB>
<ImporteCB>0000244541</ImporteCB>
<DigitoParidadCB>0</DigitoParidadCB>
</CodigoBarras>
</DatosPiePaginaRLC>
</Liquidacion>
</ReciboLiquidacionCotizaciones>
<?xml version="1.0" encoding="ISO-8859-1"?>
<DCL xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.seg-social.es/creta/esquemas/V100/DCL" xsi:schemaLocation="http://www.seg-social.es/creta/esquemas/V100/DCL DCL.xsd">
<Autorizado>12345678</Autorizado>
<Liquidacion>
<Ccc>
<Regimen>0111</Regimen>
<Provincia>28</Provincia>
<Numero>170951274</Numero>
</Ccc>
<PeriodoDesde>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoDesde>
<PeriodoHasta>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoHasta>
<Tipo>L00</Tipo>
<FechaHoraRecaudacion>
<FechaRecaudacion>
<Dia>20</Dia>
<Mes>05</Mes>
<Anho>2013</Anho>
</FechaRecaudacion>
<HoraRecaudacion>152514</HoraRecaudacion>
</FechaHoraRecaudacion>
<TipoDCL>T</TipoDCL>
<DatosCabeceraDCL>
<AlcanceCalculo>Total</AlcanceCalculo>
<CuotaALiquidar>Total</CuotaALiquidar>
<DatosEnvio>
<Autorizado>12345678</Autorizado>
<Envio>1335123456</Envio>
</DatosEnvio>
<DatosLiquidacion>
<Ccc>
<Regimen>0111</Regimen>
<Provincia>28</Provincia>
<Numero>170951274</Numero>
</Ccc>
<PeriodoDesde>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoDesde>
<PeriodoHasta>
<Mes>04</Mes>
<Anho>2013</Anho>
</PeriodoHasta>
<DescripcionTipoLiquidacion>L00 - Liquidacion normal</DescripcionTipoLiquidacion>
<RazonSocial>GALERIA COMERCIAL EL CORTE ALCALAINO</RazonSocial>
<Empresario>
<TipoIpf>9</TipoIpf>
<NumeroIpf>0B34987321</NumeroIpf>
</Empresario>
<EntidadAtEp>099 MUTUA DE ACCIDENTES DE TRABAJO</EntidadAtEp>
<NumeroTrabajadores>8</NumeroTrabajadores>
<NumeroLiquidacion>28201312345678912</NumeroLiquidacion>
</DatosLiquidacion>
</DatosCabeceraDCL>
<DatosCuerpoDCL>
<LineasDCL>
<LineaDCL>
<DescripcionLDCL>CONTINGENCIAS COMUNES</DescripcionLDCL>
<BaseLDCL>1160448</BaseLDCL>
<ImporteLDCL>328407</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>REDUCCIONES A CARGO DE LA TGSS</DescripcionLDCL>
<ImporteLDCL>196137</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>LIQUIDO CONTINGENCIAS COMUNES</DescripcionLDCL>
<ImporteLDCL>132270</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>IT DE ACCIDENTES DE TRABAJO</DescripcionLDCL>
<BaseLDCL>1160448</BaseLDCL>
<ImporteLDCL>12011</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>IMS DE ACCIDENTES DE TRABAJO</DescripcionLDCL>
<BaseLDCL>1160448</BaseLDCL>
<ImporteLDCL>8005</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>LIQUIDO DE ACCIDENTES DE TRABAJO</DescripcionLDCL>
<ImporteLDCL>20016</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>CUOTA DE OTRAS COTIZACIONES</DescripcionLDCL>
<BaseLDCL>1160448</BaseLDCL>
<ImporteLDCL>92255</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>LIQUIDO DE OTRAS COTIZACIONES</DescripcionLDCL>
<ImporteLDCL>92255</ImporteLDCL>
</LineaDCL>
<LineaDCL>
<DescripcionLDCL>LIQUIDO DE TOTALES</DescripcionLDCL>
<ImporteLDCL>244541</ImporteLDCL>
</LineaDCL>
</LineasDCL>
</DatosCuerpoDCL>
</Liquidacion>
</DCL>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema targetNamespace="http://www.seg-social.es/creta/esquemas/V110/Respuesta" xmlns="http://www.seg-social.es/creta/esquemas/V110/Respuesta" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Respuesta">
<xs:complexType>
<xs:sequence>
<xs:element ref="Autorizado"/>
<xs:element ref="ReferenciaExterna"/>
<xs:element ref="IndicadorRectificacion" minOccurs="0"/>
<xs:element ref="Errores" minOccurs="0"/>
<xs:choice>
<xs:element ref="Liquidacion" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="AccionDatosBancarios" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Autorizado">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d\d\d\d\d\d\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ReferenciaExterna">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="IndicadorRectificacion">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="S"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Liquidacion">
<xs:complexType>
<xs:sequence>
<xs:element ref="Ccc"/>
<xs:choice>
<xs:sequence>
<xs:element ref="CccConcertado"/>
<xs:element ref="PeriodoDesde"/>
<xs:element ref="PeriodoHasta"/>
<xs:element ref="Tipo"/>
<xs:element ref="FechaControl" minOccurs="0"/>
</xs:sequence>
<xs:sequence>
<xs:element ref="PeriodoDesde"/>
<xs:element ref="PeriodoHasta"/>
<xs:element ref="Tipo"/>
<xs:choice>
<xs:element ref="FechaControl" minOccurs="0"/>
<xs:element ref="AceptarBasesAnteriores" minOccurs="0"/>
</xs:choice>
</xs:sequence>
</xs:choice>
<xs:element ref="FechaHoraRecaudacion"/>
<xs:element ref="NumeroLiquidacion" minOccurs="0"/>
<xs:sequence>
<xs:element ref="Errores" minOccurs="0"/>
<xs:element ref="DatosLiquidacion" minOccurs="0"/>
<xs:element ref="LiquidacionMes" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="DatosNoTratados" minOccurs="0"/>
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="CtaCot">
<xs:sequence>
<xs:element ref="Regimen"/>
<xs:element ref="Provincia"/>
<xs:element ref="Numero"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Ccc" type="CtaCot"/>
<xs:element name="CccConcertado" type="CtaCot"/>
<xs:element name="Regimen">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d\d\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Provincia">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Numero">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d{9}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="PeriodoDesde" type="Periodo"/>
<xs:element name="PeriodoHasta" type="Periodo"/>
<xs:element name="Tipo">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="3"/>
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="FechaControl" type="Periodo"/>
<xs:complexType name="Periodo">
<xs:sequence>
<xs:element ref="Mes"/>
<xs:element ref="Anho"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Mes">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="12"/>
<xs:enumeration value="11"/>
<xs:enumeration value="10"/>
<xs:enumeration value="09"/>
<xs:enumeration value="08"/>
<xs:enumeration value="07"/>
<xs:enumeration value="06"/>
<xs:enumeration value="05"/>
<xs:enumeration value="04"/>
<xs:enumeration value="03"/>
<xs:enumeration value="02"/>
<xs:enumeration value="01"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Anho">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="4"/>
<xs:pattern value="\d{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="AceptarBasesAnteriores">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="S"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="FechaHoraRecaudacion">
<xs:complexType>
<xs:sequence>
<xs:element ref="FechaRecaudacion"/>
<xs:element ref="HoraRecaudacion"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="FechaRecaudacion" type="Fecha"/>
<xs:element name="HoraRecaudacion">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="6"/>
<xs:pattern value="\d{6}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NumeroLiquidacion">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="17"/>
<xs:pattern value="\d{17}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="DatosLiquidacion">
<xs:complexType>
<xs:sequence>
<xs:element ref="Dato" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DatosMes">
<xs:complexType>
<xs:sequence>
<xs:element ref="Dato" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Dato">
<xs:complexType>
<xs:sequence>
<xs:element ref="TipoDato"/>
<xs:element ref="Codigo"/>
<xs:element ref="Valor"/>
<xs:element ref="Errores"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="TipoDato">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="1"/>
<xs:enumeration value="C"/>
<xs:enumeration value="H"/>
<xs:enumeration value="I"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Codigo">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Valor">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="11"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="LiquidacionMes">
<xs:complexType>
<xs:sequence>
<xs:element ref="MesLiquidativo"/>
<xs:choice>
<xs:element ref="Errores"/>
<xs:sequence>
<xs:element ref="DatosMes"/>
<xs:element ref="Trabajadores" minOccurs="0"/>
</xs:sequence>
<xs:element ref="Trabajadores"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="MesLiquidativo" type="Periodo"/>
<xs:element name="Trabajadores">
<xs:complexType>
<xs:sequence>
<xs:element ref="Trabajador" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Trabajador">
<xs:complexType>
<xs:sequence>
<xs:element ref="Naf"/>
<xs:element ref="Errores" minOccurs="0"/>
<xs:element ref="Tramos" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Naf">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d{12}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Tramos">
<xs:complexType>
<xs:sequence>
<xs:element ref="Tramo" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Tramo">
<xs:complexType>
<xs:sequence>
<xs:element ref="FechaDesdeAlta" minOccurs="0"/>
<xs:element ref="FechaHastaAlta" minOccurs="0"/>
<xs:element ref="FechaDesde"/>
<xs:element ref="FechaHasta"/>
<xs:choice>
<xs:sequence>
<xs:element ref="MarcaBorrado" minOccurs="0"/>
<xs:element ref="Errores"/>
<xs:element ref="DatosTramo" minOccurs="0"/>
</xs:sequence>
<xs:element ref="DatosTramo"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Fecha">
<xs:sequence>
<xs:element ref="Dia"/>
<xs:element ref="Mes"/>
<xs:element ref="Anho"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Dia">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="2"/>
<xs:pattern value="\d\d"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="FechaDesde" type="Fecha"/>
<xs:element name="FechaHasta" type="Fecha"/>
<xs:element name="FechaDesdeAlta" type="Fecha"/>
<xs:element name="FechaHastaAlta" type="Fecha"/>
<xs:element name="MarcaBorrado">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="S"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="DatosTramo">
<xs:complexType>
<xs:sequence>
<xs:element ref="Dato" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Errores">
<xs:complexType>
<xs:sequence>
<xs:element ref="Error" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Error">
<xs:complexType>
<xs:sequence>
<xs:element ref="CodigoErr"/>
<xs:element ref="Descripcion"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CodigoErr">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Descripcion" type="xs:string"/>
<xs:element name="DatosNoTratados">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element ref="DatosLiquidacion"/>
<xs:element ref="LiquidacionMes" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:element ref="LiquidacionMes" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="AccionDatosBancarios">
<xs:complexType>
<xs:sequence>
<xs:element ref="Ccc"/>
<xs:element ref="TipoMovimiento" minOccurs="0"/>
<xs:element ref="TipoAccion" minOccurs="0"/>
<xs:element ref="DatosBancarios" minOccurs="0"/>
<xs:element ref="Errores"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="TipoMovimiento">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="C"/>
<xs:enumeration value="S"/>
<xs:enumeration value="A"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="TipoAccion">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="DatosBancarios">
<xs:complexType>
<xs:sequence>
<xs:element ref="IBAN"/>
<xs:element ref="Titular"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="IBAN">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="24"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Titular">
<xs:complexType>
<xs:sequence>
<xs:element ref="IpfTitular"/>
<xs:element ref="NombreTitular"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="IpfTitular" type="Ipf"/>
<xs:complexType name="Ipf">
<xs:sequence>
<xs:element ref="TipoIpf"/>
<xs:element ref="NumeroIpf"/>
</xs:sequence>
</xs:complexType>
<xs:element name="TipoIpf">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\w"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NumeroIpf">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\w{10}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NombreTitular">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="55"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
public static bool ValidateXmlWithXsd_3_OK(string filename, string pathXSD)
{
if (!File.Exists(filename)) return false;
// No se puede cargar el esquema para el espacio de nombres 'http://www.seg-social.es/creta/esquemas/V100/ReciboLiquidacionCotizaciones' - No se pudo encontrar el archivo 'C:\TFS\Adecco.CRETA\TestResults\Deploy_Enrique.Prados 2017-02-24 08_16_31\Out\RLC.xsd'.
// Funciona si DCL.xml y DCL.xsd están en el mismo directorio !!!
var schemas = new XmlSchemaSet();
//schemas.Add(TGSSxmlns, pathXSD); // Sin schemas o con ellos funciona
schemas.Add(null, pathXSD);
var erroresValidacion = new StringBuilder();
//if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
// throw new Exception("Error de validacion del archivo XML con el esquema XSD, " + erroresValidacion.ToString());
// configure the xmlreader validation to use inline schema.
var config = new XmlReaderSettings();
config.ValidationType = ValidationType.Schema;
config.Schemas = schemas;
config.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
config.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
config.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
//config.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
config.ValidationEventHandler += (o, args) =>
{
erroresValidacion.AppendLine("<br/>" + args.Message);
if (args.Severity == XmlSeverityType.Warning)
Console.WriteLine("Warning: Matching schema not found. No validation occurred." + args.Message);
else // Error
Console.WriteLine("Validation error: " + args.Message);
};
// Get the XmlReader object with the configured settings.
XmlReader reader3 = XmlReader.Create(filename, config);
// Parsing the file will cause the validation to occur.
while (reader3.Read()) ;
if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
return false;
return true;
}
public static bool ValidateXmlWithXsd_8_KO(string filename, string pathXSD)
{
if (!File.Exists(filename)) return false;
var schemas = new XmlSchemaSet();
//schemas.Add(TGSSxmlns, pathXSD); // Sin schemas o con ellos no afecta parece
schemas.Add(null, pathXSD);
var erroresValidacion = new StringBuilder();
// Load document
XDocument doc = XDocument.Load(filename);
// Extract value of xsi:noNamespaceSchemaLocation
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
string schemaURI = (string)doc.Root.Attribute(xsi + "noNamespaceSchemaLocation");
// Create schema set
var schemas1 = new XmlSchemaSet();
if (schemaURI != null) schemas1.Add("Schemas", schemaURI);
// Validate
doc.Validate(schemas1, (o, e) =>
{
erroresValidacion.AppendLine("<br/>" + e.Message);
Console.WriteLine("{0}", e.Message);
});
if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
return false;
return true;
}
public static bool ValidateXmlWithXsd_7_KO(string filename, string pathXSD)
{
if (!File.Exists(filename)) return false;
var schemas = new XmlSchemaSet();
//schemas.Add(TGSSxmlns, pathXSD); // Sin schemas o con ellos no afecta parece
schemas.Add(null, pathXSD);
var erroresValidacion = new StringBuilder();
var settings = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Document,
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ProcessSchemaLocation |
XmlSchemaValidationFlags.ProcessInlineSchema,
};
int warnings = 0;
int errors = 0;
settings.ValidationEventHandler += (obj, ea) =>
{
erroresValidacion.AppendLine("<br/>" + ea.Message);
if (ea.Severity == XmlSeverityType.Warning)
{
++warnings;
}
else
{
++errors;
}
};
settings.Schemas = schemas;
//The important part is that you DO NOT specify the targetNamespace. I used the overload XmlShemaSet.Add(string targetNamespace, string schemaUri).
//schemas.Add( null, "c:\test\mySchema.xsd" );
var inputDocInString = File.ReadAllText(filename);
XmlReader xvr = XmlReader.Create(new StringReader(inputDocInString), settings);
try
{
while (xvr.Read())
{
// do nothing
}
if (0 != errors)
{
Console.WriteLine("\nFailed to load XML, {0} error(s) and {1} warning(s).", errors, warnings);
}
else if (0 != warnings)
{
Console.WriteLine("\nLoaded XML with {0} warning(s).", warnings);
}
else
{
System.Console.WriteLine("Loaded XML OK");
}
Console.WriteLine("\nSchemas loaded durring validation:");
//////ListSchemas(xvr.Schemas, 1);
}
catch (System.Xml.Schema.XmlSchemaException e)
{
System.Console.Error.WriteLine("Failed to read XML: {0}", e.Message);
}
catch (System.Xml.XmlException e)
{
System.Console.Error.WriteLine("XML Error: {0}", e.Message);
}
catch (System.IO.IOException e)
{
System.Console.Error.WriteLine("IO error: {0}", e.Message);
}
if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
return false;
return true;
}
public static bool ValidateXmlWithXsd_6_KO(string filename, string pathXSD)
{
if (!File.Exists(filename)) return false;
var schemas = new XmlSchemaSet();
//schemas.Add(TGSSxmlns, pathXSD); // Sin schemas o con ellos no afecta parece
//schemas.Add(null, pathXSD);
var erroresValidacion = new StringBuilder();
var xsd = new XmlSchema();
xsd.SourceUri = pathXSD;
XmlSchemaSet ss = new XmlSchemaSet();
//ss.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
ss.ValidationEventHandler += (o, e) =>
{
erroresValidacion.AppendLine("<br/>" + e.Message);
};
ss.Add(null, pathXSD);
if (ss.Count > 0)
{
XmlReaderSettings settings1 = new XmlReaderSettings();
settings1.ValidationType = ValidationType.Schema;
settings1.Schemas.Add(ss);
settings1.Schemas.Compile();
//settings1.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
settings1.ValidationEventHandler += (o, e) =>
{
erroresValidacion.AppendLine("<br/>" + e.Message);
};
var r = new XmlTextReader(filename);
using (var reader11 = XmlReader.Create(r, settings1))
{
while (reader11.Read())
{
}
}
}
if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
return false;
return true;
}
public static bool ValidateXmlWithXsd_5_KO(string filename, string pathXSD)
{
if (!File.Exists(filename)) return false;
var schemas = new XmlSchemaSet();
//schemas.Add(TGSSxmlns, pathXSD); // Sin schemas o con ellos no afecta parece
schemas.Add(null, pathXSD);
var erroresValidacion = new StringBuilder();
// Set the validation settings.
var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = schemas;
//settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
settings.ValidationEventHandler += (o, args) =>
{
erroresValidacion.AppendLine("<br/>" + args.Message);
if (args.Severity == XmlSeverityType.Warning)
Console.WriteLine("Warning: Matching schema not found. No validation occurred." + args.Message);
else // Error
Console.WriteLine("Validation error: " + args.Message);
};
settings.IgnoreWhitespace = true;
// Create the XmlReader object.
XmlReader reader1 = XmlReader.Create(filename, settings);
// Parse the file.
while (reader1.Read())
{
System.Console.WriteLine("{0}, {1}: {2} ", reader1.NodeType, reader1.Name, reader1.Value);
}
if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
return false;
return true;
}
public static bool ValidateXmlWithXsd_2_KO(string filename, string pathXSD)
{
if (!File.Exists(filename)) return false;
var schemas = new XmlSchemaSet();
//schemas.Add(TGSSxmlns, pathXSD);
schemas.Add(null, pathXSD);
var erroresValidacion = new StringBuilder();
XDocument docValidar = System.Xml.Linq.XDocument.Load(filename);
docValidar.Validate(schemas, (o, e) =>
{
erroresValidacion.AppendLine("<br/>" + e.Message);
});
if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
return false;
return true;
}
public static bool ValidateXmlWithXsd_1_KO(string filename, string pathXSD)
{
if (!File.Exists(filename)) return false;
var targetNamespace = "http://www.seg-social.es/creta/esquemas/V100/DCL";
var schemas = new XmlSchemaSet();
//schemas.Add(TGSSxmlns, pathXSD);
//schemas.Add(null, pathXSD);
//////////schemas.Add("", XmlReader.Create(new StringReader(pathXSD)));
///////////schemas.Add(targetNamespace, XmlReader.Create(new StringReader(pathXSD)));
////schemas.Add(targetNamespace, pathXSD);
using (var stream = new StreamReader(pathXSD))
{
// the null here is a validation call back for the XSD itself, unless you
// specifically want to handle XSD validation errors, I just pass a null and let
// an exception get thrown as there usually isn't much you can do with an error in
// the XSD itself
schemas.Add(XmlSchema.Read(stream, null));
stream.Close();
stream.Dispose();
}
////GC.Collect();
schemas = null; // this needs to be accessible to the methods below, so should be a class level field or property
using (var fs = new FileStream(pathXSD, FileMode.Open))
{
var schema = XmlSchema.Read(fs, null);
schemas = new XmlSchemaSet();
schemas.Add(schema);
schemas.Compile();
fs.Close();
////fs.Flush();
}
Console.WriteLine("Attempting to validate");
XDocument custOrdDoc = XDocument.Load(filename);
bool errors = false;
custOrdDoc.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
});
Console.WriteLine("custOrdDoc {0}", errors ? "did not validate" : "validated");
var reader = XmlReader.Create(filename);
var document = XDocument.Load(reader);
XmlSchemaException firstException = null;
var erroresValidacion = new StringBuilder();
// the following call to Validate succeeds.
document.Validate(schemas, (o, vargs) =>
{
erroresValidacion.AppendLine("<br/>" + vargs.Message);
//Console.WriteLine("{0}", vargs.Message);
//// errors = true;
//if (vargs.Severity == XmlSeverityType.Warning)
//{
// Console.WriteLine(vargs.Message);
//}
//else
//{
// if (firstException == null)
// {
// firstException = vargs.Exception;
// }
// Console.WriteLine(vargs.Exception.ToString());
//}
});
if (!string.IsNullOrWhiteSpace(erroresValidacion.ToString()))
return false;
//throw new Exception("Error de validacion del archivo XML con el esquema XSD, " + erroresValidacion.ToString());
return true;
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
namespace xxxx.Core
{
public class XmlValidator
{
private int nErrors = 0;
private string strErrorMsg = string.Empty;
public string Errors { get { return strErrorMsg; } }
public void ValidationHandler(object sender, ValidationEventArgs args)
{
nErrors++;
strErrorMsg = strErrorMsg + args.Message + "\r\n";
}
public bool IsValidXml(string strXml/*xml in text*/, string strXsdLocation /*Xsd location*/)
{
bool bStatus = false;
try
{
// Declare local objects
using (var xtrReader = new XmlTextReader(strXsdLocation))
{
var xcSchemaCollection = new XmlSchemaCollection();
xcSchemaCollection.Add(null/*add your namespace string*/, xtrReader);//Add multiple schemas if you want.
var vrValidator = new XmlValidatingReader(strXml, XmlNodeType.Document, null);
vrValidator.Schemas.Add(xcSchemaCollection);
// Add validation event handler
vrValidator.ValidationType = ValidationType.Schema;
vrValidator.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
//Actual validation, read conforming the schema.
while (vrValidator.Read()) ;
vrValidator.Close();//Cleanup
//Exception if error.
if (nErrors > 0) { throw new Exception(strErrorMsg); }
else { bStatus = true; }//Success
}
}
catch (Exception error) { bStatus = false; }
return bStatus;
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
namespace xxxx.Core
{
// https://satalketo.com/2013/09/15/validating-xml-against-xsd/
public class XsdValidator
{
public List<XmlSchema> Schemas { get; set; }
public List<String> Errors { get; set; }
public List<String> Warnings { get; set; }
public XsdValidator()
{
Schemas = new List<XmlSchema>();
}
/// <summary>
/// Add a schema to be used during the validation of the XML document
/// </summary>
/// <param name="schemaFileLocation">The file path for the XSD schema file to be added for validation</param>
/// <returns>True if the schema file was successfully loaded, else false (if false, view Errors/Warnings for reason why)</returns>
public bool AddSchema(string schemaFileLocation)
{
if (String.IsNullOrEmpty(schemaFileLocation)) return false;
if (!File.Exists(schemaFileLocation)) return false;
// Reset the Error/Warning collections
Errors = new List<string>();
Warnings = new List<string>();
XmlSchema schema;
using (var fs = File.OpenRead(schemaFileLocation))
{
schema = XmlSchema.Read(fs, ValidationEventHandler);
}
var isValid = !Errors.Any() && !Warnings.Any();
if (isValid)
{
Schemas.Add(schema);
}
return isValid;
}
/// <summary>
/// Perform the XSD validation against the specified XML document
/// </summary>
/// <param name="xmlLocation">The full file path of the file to be validated</param>
/// <returns>True if the XML file conforms to the schemas, else false</returns>
public bool IsValid(string xmlLocation)
{
if (!File.Exists(xmlLocation))
{
throw new FileNotFoundException("The specified XML file does not exist", xmlLocation);
}
using (var xmlStream = File.OpenRead(xmlLocation))
{
return IsValid(xmlStream);
}
}
/// <summary>
/// Perform the XSD validation against the supplied XML stream
/// </summary>
/// <param name="xmlStream">The XML stream to be validated</param>
/// <returns>True is the XML stream conforms to the schemas, else false</returns>
private bool IsValid(Stream xmlStream)
{
// Reset the Error/Warning collections
Errors = new List<string>();
Warnings = new List<string>();
var settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema
};
settings.ValidationEventHandler += ValidationEventHandler;
foreach (var xmlSchema in Schemas)
{
settings.Schemas.Add(xmlSchema);
}
var xmlFile = XmlReader.Create(xmlStream, settings);
try
{
while (xmlFile.Read()) { }
}
catch (XmlException xex)
{
Errors.Add(xex.Message);
}
return !Errors.Any() && !Warnings.Any();
}
private void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Errors.Add(e.Message);
break;
case XmlSeverityType.Warning:
Warnings.Add(e.Message);
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment