Re: validate xml using xsd

Tech-Archive recommends: Speed Up your PC by fixing your registry



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/
.



Relevant Pages

  • Re: What is the best way to parse and validate an arbitary XML documen
    ... (New documents presented to my system are schema based while the ... XmlReader.Create method but these readers must be configured as either DTD ... XmlReaderSettings settings; ...
    (microsoft.public.dotnet.xml)
  • Re: Validating a SAML 1.1 Response
    ... So to have the XmlSchemaSet process that schema you need to load it explicitly with an XmlReader and XmlReaderSettings having ProhibitDtd as false. ... For better error messages/diagnostics you might want to set up a ValidationEventHandler on the Schemas property of your XmlReaderSettings, that way you get a warning that the import did not work. ... XmlReaderSettings settings = new XmlReaderSettings; ... settings.Schemas.ValidationEventHandler += delegate(object sender, ValidationEventArgs vargs) ...
    (microsoft.public.dotnet.xml)
  • Re: Validating a SAML 1.1 Response
    ... So to have the XmlSchemaSet process that schema you need to load it explicitly with an XmlReader and XmlReaderSettings having ProhibitDtd as false. ... For better error messages/diagnostics you might want to set up a ValidationEventHandler on the Schemas property of your XmlReaderSettings, that way you get a warning that the import did not work. ... XmlReaderSettings have ProhibitDtd set to false, that way the imported schemas will be loaded with the same settings and that way the import and the XmlSchemaSet compilation works so that you will get the validation error then for your XML document: ... settings.Schemas.ValidationEventHandler += delegate(object sender, ValidationEventArgs vargs) ...
    (microsoft.public.dotnet.xml)
  • Re: Frage zu Configuration Application Block
    ... Eigene Klasse mit den Config-Werten erstellen (Attribute ... Klasse Settings: ... gelich das xml File erstellen, ... section exists in one of the files and that the section 'ProSaSoftSettings' ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Registry replacement advice
    ... I use the registry for very simple stuff, ... INI files for config settings is probably your best bet. ... class file which wraps the XML library to make it easier to use without ... |>> the recordset is done and it's saved back to XML file. ...
    (microsoft.public.vb.general.discussion)