Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. attribute restriction [modified] [Solved]

attribute restriction [modified] [Solved]

Scheduled Pinned Locked Moved XML / XSL
xmlcsharpdatabasehelpquestion
1 Posts 1 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    V 0
    wrote on last edited by
    #1

    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
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups