validate xml using xsd

  • Thread starter Alhambra Eidos Desarrollo
  • Start date
A

Alhambra Eidos Desarrollo

Hi all,

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)



any help about it ? any sample code source ?

Thanks in advance, king regards
 
M

Martin Honnen

Alhambra said:
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()) { }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top