954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

validate xml string against XSD

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"?>

....

Note : this is my xml string

...

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 .

antobx
Newbie Poster
1 post since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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

BinodSuman
Newbie Poster
7 posts since May 2009
Reputation Points: 12
Solved Threads: 0
 

Welcome AntoBx, and Binod. You both must read the http://www.daniweb.com/forums/announcement18-4.html

Use BB code to post your code.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You