2017-07-20 6 views
0

私たちのプロジェクトでは、XSDから生成されたクラスがあります。現在、XSDファイルパスを使用してXSDに対してXMLを検証しています。C#生成されたXsd生成クラスに対してXMLを検証

は、いくつかのXSDの存在であり、私たちはただ好きで、データベースに保存された数だけ右いずれかを選択します。

"C:/プロジェクト/ XSD /レポート/ Report_ 1.7の.xsd"
」 C:/プロジェクト/ XSD /レポート/ Report_ 1.8それは、このようなプロジェクトでのファイル・パスを持って開始したとき、私は少し神経質になっているので、「

を.XSD。このユースケースのベストプラクティスはありますか?生成されたC#クラスに対してxmlを直接検証するようなものです。私の現在のコード:

private static string GetXsdPath(SchemaType aSchemaType, string aTransferRevision) 
    { 
     var lFileBeginnName = XsdStrategies.XsdService.GetXsdName(aSchemaType); 
     var lDirectoryName = XsdStrategies.XsdService.GetDirectoryName(aSchemaType); 
     string lRoot = HttpContext.Current.Server.MapPath("~"); 
     string lFullRootPath = Path.GetFullPath(Path.Combine(lRoot, @"../")); 

     return string.Format(
      CultureInfo.CurrentCulture, @"{0}/Reports/{1}/Report_V{2}.xsd", 
      lFullRootPath, 
      lDirectoryName, 
      aTransferRevision); 
    } 

    public bool IsValidXml(string aXmlContent, string aXsdFilePath, XNamespace aNamespaceName) 
    { 
     try 
     { 
      if (aNamespaceName == null) 
      { 
       this.Logger.AddLogEntry(LogLevel.Error, "Namespace is null."); 
       return false; 
      } 

      var lXdoc = XDocument.Parse(aXmlContent); 
      var lSchemas = new XmlSchemaSet(); 
      lSchemas.Add(aNamespaceName.NamespaceName, aXsdFilePath); 

      // xDoc Validate throws an excption if xml not conforms xsd. 
      lXdoc.Validate(lSchemas, null); 
     } 
     catch (XmlSchemaValidationException lEx) 
     { 
      this.Logger.AddLogEntry(LogLevel.Error, $"The Xml is not valid against the Xsd: {lEx}"); 
      return false; 
     } 
     catch (XmlSchemaException lEx) 
     { 
      this.Logger.AddLogEntry(LogLevel.Error, $"Therse is something wrong in the Schema-Version from Xml and Xsd: {lEx}"); 
      return false; 
     } 
     catch (XmlException lEx) 
     { 
      this.Logger.AddLogEntry(LogLevel.Error, $"A generic Error occured durring Xml against Xsd validation: {lEx}"); 
      return false; 
     } 

     return true; 
    } 

答えて

0

私はあなたが非常に正しい、これは悪い考えであることをしている、あなたはデータベースだけではなく、パスに直接XSDデータを保存することをお勧め。

XSDデータは、たとえば、MS SQL ServerのNVarCharデータ型のようなものに格納できます。

関連する問題