简体   繁体   English

C#针对生成的Xsd生成的类验证XML

[英]C# validate XML against generated Xsd generated class

In our project, we have classes generated from XSD's. 在我们的项目中,我们具有从XSD生成的类。 Currently we are validating the XML against the XSD by the XSD file path. 当前,我们正在通过XSD文件路径针对XSD验证XML。

There are several XSD's and we select the right one by a number stored in the Database just like: 有多个XSD,我们通过存储在数据库中的数字选择正确的XSD,如下所示:

"C:/Projects/XSD/Reports/Report_ 1.7 .xsd" “ C:/ Projects / XSD / Reports / Report_ 1.7 .xsd”
"C:/Projects/XSD/Reports/Report_ 1.8 .xsd" “ C:/ Projects / XSD / Reports / Report_ 1.8 .xsd”

Because I become a little bit nervous when it starts to have file paths in a project like this. 因为当它在这样的项目中开始具有文件路径时,我有点紧张。 Is there a best practice for this use-case? 此用例是否有最佳实践? Something like validating the xml against the generated C# class directly. 直接针对生成的C#类验证xml之类的东西。 My current Code: 我当前的代码:

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

I would suggest you store the XSD data directly in the database rather than just the path, you're very much correct that this is a bad idea. 我建议您将XSD数据直接存储在数据库中,而不是仅将路径存储在数据库中,您非常正确的认为这是一个坏主意。

You can store the XSD data in something like an NVarChar data type in MS SQL Server, for example. 例如,您可以将XSD数据存储为类似MS SQL Server中的NVarChar数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM