2010-05-21 141 views
0

作爲我的問題transforming large Xml files的後續,我現在需要驗證架構。驗證大型Xml文件

我使用這個擴展方法,它可以清楚地在爲它不能正常工作得到改善或者

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

什麼是錯誤?意思是這種方法拋出一個異常,如果是的話是什麼類型? 還是有錯誤,那麼你沒有讓他們回來? 答案可能因錯誤而異。 – 2010-05-21 19:34:26

回答

3

試試這個:

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; 
} 

試試這個:

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

如果你的xsd是基於源的一個子集的元素,這會工作嗎? – 2010-05-21 19:36:37

+0

它將起作用或不起作用,具體取決於源文件中是否有模式。 – 2010-05-21 19:39:24

+0

我有這個問題是源碼。文檔爲空 – CaffGeek 2010-05-21 19:51:20