Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Validate XML against XSD with imported xsd's

$
0
0

Hello,

I'm stuck for a while attempting to validate a xml against some xsd's. In the code below you can find my xsd's structure: (I need to use this structure in order to reuse the baseXsd's files.

CommonTypes.xsd

<?xml version="1.0" encoding="utf-8"?><xs:schema id="CommonTypes" targetNamespace="http://test.com/CommonTypes"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="qualified"><xs:simpleType name="NonEmptyString"><xs:restriction base="xs:string"><xs:minLength value="1"/></xs:restriction></xs:simpleType></xs:schema>

CommonLog.xsd

<?xml version="1.0" encoding="utf-8"?><xs:schema id="CommonLogConfig"
    targetNamespace="http://test.com/CommonLogConfig"
    xmlns:cmn="http://test.com/CommonTypes"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="qualified"><xs:import schemaLocation="..\Base\CommonTypes.xsd" namespace="http://test.com/CommonTypes" /><xs:simpleType name="LogName"><xs:restriction base="xs:string"/></xs:simpleType><xs:simpleType name="LogPath"><xs:restriction base="cmn:NonEmptyString"/></xs:simpleType></xs:schema>

LogConfig.xsd

<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:mlc="http://test.com/CommonLogConfig"
           targetNamespace="component1_LogConfig"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="qualified"><xs:import schemaLocation="..\Base\CommonLogConfig.xsd" namespace="http://test.com/CommonLogConfig" /><xs:element name="root"><xs:complexType><xs:sequence><xs:element name="default"><xs:complexType><xs:all><xs:element name="LogName" type="mlc:LogName" fixed="component.name" /><xs:element name="LogPath" type="mlc:LogPath"/></xs:all></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:schema>

LogConfig.xml

<?xml version="1.0" encoding="utf-8"?><root xmlns="component1_LogConfig"><default><LogName>component.name</LogName><LogPath></LogPath></default></root>

As you can probably notice, the <LogPath> key on the LogConfig is empty, but, it is defined as a NonEmptyString type. That is the way I'm validating the xml against xsd:

FileStream stream = new FileStream("LogConfig.xml", FileMode.Open);
XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

vr.Schemas.Add(null, "LogConfig.xsd");
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

while (vr.Read()) ;

The validationCallBack is not being fired.

I suppose it is possible since the visual studio show me the error at compile time:


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>