attribute restriction [modified] [Solved]
XML / XSL
1
Posts
1
Posters
3
Views
1
Watching
-
I wrote a small .Net app that validates an XML with an XSD. It looks like it is working, until I did the following below. the XML is validated although the attribute is restricted to 8 chars. What could I be doing wrong? many thanks. [Solved] like this:
public static void Validate(string xml\_file, string xsd\_file, string xmlnamespace){ schemaexception = null; validationexception = null; XmlReader reader = null; XmlReaderSettings xmlreadersettings = new XmlReaderSettings(); XmlSchemaSet myschema = new XmlSchemaSet(); try{ xmlreadersettings.Schemas.Add(xmlnamespace, xsd\_file); xmlreadersettings.ValidationType = ValidationType.Schema; xmlreadersettings.ValidationEventHandler += new ValidationEventHandler(xmlreadersettings\_ValidationEventHandler); reader = XmlReader.Create(xml\_file, xmlreadersettings); while(reader.Read()); } //end try catch (XmlException XmlExp){ schemaexception = XmlExp; } //end catch catch(XmlSchemaException XmlSchExp){ schemaexception = XmlSchExp; } //end catch catch(Exception GenExp){ schemaexception = GenExp; } //end catch finally{ reader.Close(); } //end finally }
[/Solved] [EDIT]I found an online tool dat also validates the xml against the xsd and that seems to work. it even says what the exact error is. I'll have to have a look at the .Net code. If anyone has a good article about it, let me know. (I'll also, of course, search for myself)[/EDIT] xml
<MyCode value="abcdefSSSgh"/>
xsd
<xs:element name="MyCode" minOccurs="1"> <xs:complexType> <xs:attribute name="value" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element>
.Net code
public static Exception Validate(string xml\_file, string xsd\_file, string xmlnamespace){ Exception xmlexception = null; XmlReader reader = null; XmlSchemaSet myschema = new XmlSchemaSet(); try{ reader = XmlReader.Create(xml\_file); myschema.Add(xmlnamespace, xsd\_file); reade