Hi ,
I create a xml string using a xmldocument (c# 2.0 )
This is my code (more or less)

XmlElement MySearchNode = xmlDoc.CreateElement("MySearch");
xmlDoc.AppendChild(MySearchNode);
...
XmlNode node1 = xmlDoc.SelectSingleNode("/MySearch/Objects");
XmlAttribute nameAttrib = xmlDoc.CreateAttribute("result");
nameAttrib.Value = "012";
node1.Attributes.Append(nameAttrib);
xmlDoc.outerxml

I want to validate the xml string against a XSD :
XmlReader rdr = null;
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
XmlTextReader readerXml = new XmlTextReader(p_xml, XmlNodeType.Element, context);
XmlTextReader readerSchema = new XmlTextReader(p_xsdPath);
XmlSchema schema = new XmlSchema();
schema = XmlSchema.Read(readerSchema, new
ValidationEventHandler(Schema_ValidationEventHandler));
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
ReaderSettings.ValidationType = ValidationType.Schema;
ReaderSettings.Schemas.Add(schema);
ReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;
ReaderSettings.ValidationEventHandler += new
ValidationEventHandler(settings_ValidationEventHandler);
rdr = XmlReader.Create(readerXml, ReaderSettings);

this is my XSD
(a part of it)
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MySearch">
<xs:complexType>
<xs:sequence>
<xs:element name="Objects" type="tObjects" minOccurs="0" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="result" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:complexType name="tObjects">
<xs:sequence>
<xs:element name="House" type="tHouse" minOccurs="0" maxOccurs="unbounded">
</xs:element>
<xs:element name="Garden" type="tGarden" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
....

Note : this is my xml string
<MySearch>
<Objects result = '012' >
<House>
...
</House>
</Objects>
</MySearch>

when I validate I get this error :
The 'result' attribute is not declared.

I see that validation process need :

ReaderSettings.ValidationFlags = XmlSchemaValidationFlags.AllowXmlAttributes;

and

attributeFormDefault="unqualified" .

It doesn't work !! I always get the same error ..


Could you help me ? thanks in advance .

You can use this code to validate any xml file againt xsd file.

public boolean validate() throws Exception {
VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
Schema schema = factory.compileSchema(schemaURI);
Verifier verifier = schema.newVerifier();
verifier.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException saxParseEx) {
System.out.println("ERROR :: ");
saxParseEx.printStackTrace();
}
public void fatalError(SAXParseException saxParseEx) {
System.out.println("FATAL ERROR :: ");
saxParseEx.printStackTrace();
}
public void warning(SAXParseException saxParseEx) {
System.out.println("WARNING :: ");
saxParseEx.printStackTrace();
}
});
VerifierHandler handler = verifier.getVerifierHandler();
SAXWriter writer = new SAXWriter(handler);
writer.write(document);
return handler.isValid();
}

Thanks,

Binod Suman

<delete fake signature>

This article has been dead for over six months. Start a new discussion instead.