Re: validate xml using xsd
- From: Martin Honnen <mahotrash@xxxxxxxx>
- Date: Thu, 05 Mar 2009 12:58:20 +0100
Alhambra Eidos Desarrollo wrote:
I need validate an xml using xsd in .net,
my files
xml
<?xml version="1.0" encoding="UTF-8"?><fe:Facturae xmlns:fe="http://www.facturae.es/Facturae/2007/v3.1/Facturae" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><FileHeader>... (more)
xsd
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.facturae.es/Facturae/2007/v3.1/Facturae" targetNamespace="http://www.facturae.es/Facturae/2007/v3.1/Facturae" version="3.1"><xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd"/><xs:element name="Facturae"> ... (more)
You need to load the schema with a setting to allow DTDs (as the imported xmldsig schema references one):
XmlReaderSettings dtdSettings = new XmlReaderSettings();
dtdSettings.ProhibitDtd = false;
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.ValidationEventHandler += delegate(object sender, ValidationEventArgs vargs)
{
Console.WriteLine("Schema problem: Line {2}: {0}: {1}", vargs.Severity, vargs.Message, vargs.Exception.LineNumber);
};
settings.Schemas.Add(null, XmlReader.Create(@"schema.xsd", dtdSettings));
settings.ValidationEventHandler += delegate(object sender, ValidationEventArgs vargs)
{
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message);
};
using (XmlReader reader = XmlReader.Create(@"file.xml", settings))
{
while (reader.Read()) { }
}
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
.
- References:
- validate xml using xsd
- From: Alhambra Eidos Desarrollo
- validate xml using xsd
- Prev by Date: Re: [] comparision
- Next by Date: RE: Creating a derived object from a based object
- Previous by thread: validate xml using xsd
- Index(es):
Relevant Pages
|