2010-05-21 3 views
0

Suite à mon numéro transforming large Xml files, j'ai maintenant besoin de valider le schéma.Validation de grands fichiers XML

j'utilisais cette méthode d'extension, qui peut clairement être amélioré car il est ne fonctionne pas correctement soit

public static XElement ValidateXsd(this XElement source, string xsdPath) 
{ 
    var errors = new XElement("Errors"); 

    // Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx 
    var xsd = XDocument.Load(xsdPath); 
    var xml = XDocument.Load(source.CreateReader()); 

    var schemas = new XmlSchemaSet(); 
    schemas.Add("", xsd.CreateReader()); 

    if (xml.Document != null) 
    { 
     xml.Document.Validate(schemas, 
      // Validation Event/Error Handling 
      (sender, e) => 
       { 
        var message = e.Message 
         .Replace(
          "element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.", 
          "cannot be blank.") 
         .Replace(
          "is invalid according to its datatype 'size' - The Pattern constraint failed.", 
          "must be numeric.") 
         .Replace(
          "element is invalid", 
          "is invalid."); 

        errors.Add(new XElement("Error", message)); 
       } 
      ); 
    } 

    // If there were errors return them, otherwise return null 
    return errors.Elements().Count() > 0 ? errors : null; 
} 
+0

Quelle est l'erreur? Signification est cette méthode jetant une exception si oui quel type? Ou y a-t-il des erreurs alors vous ne les récupérez pas? La réponse peut varier en fonction de l'erreur. –

Répondre

3

Essayez ceci:

public static XElement ValidateXsd(this XElement source, string xsdPath) 
{ 
    var errors = new XElement("Errors"); 

    // Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx 

    var schemas = new XmlSchemaSet(); 
    using (var reader = XmlReader.Create(xsdPath)) 
    { 
     schemas.Add("", reader); 
    } 

    { 
     source.Document.Validate(
      schemas, 
      // Validation Event/Error Handling 
      (sender, e) => 
      { 
       var message = 
        e.Message.Replace(
         "element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.", 
         "cannot be blank.").Replace(
         "is invalid according to its datatype 'size' - The Pattern constraint failed.", 
         "must be numeric.").Replace("element is invalid", "is invalid."); 

       errors.Add(new XElement("Error", message)); 
      }); 
    } 

    // If there were errors return them, otherwise return null 
    return errors.Elements().Count() > 0 ? errors : null; 
} 

Essayez ceci:

using System.Linq; 
using System.Xml; 
using System.Xml.Linq; 
using System.Xml.Schema; 

    static class Extensions 
    { 
     public static XElement ValidateXsd(this XElement source, string xsdPath) 
     { 
      var errors = new XElement("Errors"); 

      // Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx 

      var schemas = new XmlSchemaSet(); 
      using (var reader = XmlReader.Create(xsdPath)) 
      { 
       schemas.Add("", reader); 
      } 

      var sourceQualifiedName = new XmlQualifiedName(source.Name.LocalName, source.Name.NamespaceName); 
      source.Validate(
       schemas.GlobalElements[sourceQualifiedName], 
       schemas, 
       // Validation Event/Error Handling 
       (sender, e) => 
       { 
        var message = 
         e.Message.Replace(
          "element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.", 
          "cannot be blank.").Replace(
          "is invalid according to its datatype 'size' - The Pattern constraint failed.", 
          "must be numeric.").Replace("element is invalid", "is invalid."); 

        errors.Add(new XElement("Error", message)); 
       }); 

      // If there were errors return them, otherwise return null 
      return errors.Elements().Count() > 0 ? errors : null; 
     } 
    } 
+0

Cela fonctionnerait-il si xsd est basé sur l'élément qui est un sous-ensemble de la source? –

+0

Cela fonctionnera ou non, selon qu'il est valide d'avoir un schéma dans votre source. –

+0

le problème que j'ai avec ceci est source.Document est nul – CaffGeek