在 ASP.Net 中上传引用 XSD 的 XML 文件

发布于 2024-08-03 09:10:27 字数 1944 浏览 1 评论 0原文

我有一个 XML 文件,正在通过普通文件上传控件上传到 ASP.Net 页面。当它启动时,我尝试验证和反序列化 XML。但是,下面的代码对于验证引用其 XSD 的 XML 文件确实非常方便,如下所示:

xsi:schemaLocation="someurl ..\localSchemaPath.xsd"

但是,如果我上传此 XML 文件,则仅上传 XML 文件,因此 ..\localSchemaPath.xsd 不存在,因此它无法验证。

即使我将 XSD 存储在本地,它仍然不太正确,因为 XML 文件可以使用如下架构位置编写:

xsi:schemaLocation="someurl ..\localSchemaPath.xsd"

或 xsi:schemaLocation="someurl localSchemaPath.xsd" 或者 xsi:schemaLocation="someurl ...................\localSchemaPath.xsd" 如果它愿意的话。

困境!

(为了解决这个问题,我从以下位置摘取了以下代码: 根据 C# 中引用的 XSD 验证 XML)

using System.Xml;
using System.Xml.Schema;
using System.IO;

public class ValidXSD
{
    public static void Main()
    {
        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

        // Create the XmlReader object.
        XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);

        // Parse the file. 
        while (reader.Read()) ;
    }

    // Display any warnings or errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);
    }
}

I have an XML file which is being uploaded to an ASP.Net page via the normal file upload control. When it gets up, I am attempting to validate and deserialize the XML. However, the code below is really very handy for validating an XML file which references it's XSD like this:

xsi:schemaLocation="someurl ..\localSchemaPath.xsd"

However, if I upload this XML file, only the XML file gets uploaded, so ..\localSchemaPath.xsd doesn't exist, so it can't validate.

Even if I stored the XSD locally, it still wouldn't be quite right as the XML file could be written with a schema location like:

xsi:schemaLocation="someurl ..\localSchemaPath.xsd"

or
xsi:schemaLocation="someurl localSchemaPath.xsd"
or
xsi:schemaLocation="someurl ..................\localSchemaPath.xsd"
if it so wished.

Dilemma!

(For the purposes of this question, I have pinched the code below from: Validating an XML against referenced XSD in C#)

using System.Xml;
using System.Xml.Schema;
using System.IO;

public class ValidXSD
{
    public static void Main()
    {
        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

        // Create the XmlReader object.
        XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);

        // Parse the file. 
        while (reader.Read()) ;
    }

    // Display any warnings or errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2024-08-10 09:10:27

下面是我用本地模式验证 xml 的一段代码:

string errors = string.Empty;

try
{
    XmlSchemaSet schemas = new XmlSchemaSet();
    schemas.Add(string.Empty, Page.MapPath("~/xml/Schema.xsd"));
    XmlDocument doc = new XmlDocument();
    doc.Schemas = schemas;
    doc.Load(Page.MapPath("~/xml/sampleXML.xml"));
    //use this line instead of the one above for a string in memory.
    //doc.InnerXml = xmlToValidate;  
    ValidationEventHandler validator = delegate(object send, ValidationEventArgs ve)
                                           {
                                               errors += "\n" + ve.Severity + ": " + ve.Message;
                                           };

    doc.Validate(validator);
}
catch (XmlException xe)
{
    errors += "\n" + xe.Message;
}
catch (XmlSchemaValidationException xe)
{
    errors += "\n" + xe.Message;
}

Here is a chunk of code I use to validate xml with a local schema:

string errors = string.Empty;

try
{
    XmlSchemaSet schemas = new XmlSchemaSet();
    schemas.Add(string.Empty, Page.MapPath("~/xml/Schema.xsd"));
    XmlDocument doc = new XmlDocument();
    doc.Schemas = schemas;
    doc.Load(Page.MapPath("~/xml/sampleXML.xml"));
    //use this line instead of the one above for a string in memory.
    //doc.InnerXml = xmlToValidate;  
    ValidationEventHandler validator = delegate(object send, ValidationEventArgs ve)
                                           {
                                               errors += "\n" + ve.Severity + ": " + ve.Message;
                                           };

    doc.Validate(validator);
}
catch (XmlException xe)
{
    errors += "\n" + xe.Message;
}
catch (XmlSchemaValidationException xe)
{
    errors += "\n" + xe.Message;
}
像极了他 2024-08-10 09:10:27

我不太清楚您是否正在尝试通用的针对任何引用模式进行验证,或者您是否有一个每次验证的特定模式,并且只是不确定如何处理引用。

如果是后者,则将架构在互联网上公开,并告诉人们通过 URI 引用它。

如果是前者,那么我建议如下:

  • 首先用户上传一个 XML 文件。
  • 解析 XML 文件以获取架构引用。使用新的上传框告诉他们“找到了对 yourSchema.xsd 的引用;请在下面上传此文件”。
  • 然后,根据上传的架构验证文件。为此,请修改 settings 对象的 Schemas 属性,而不是修改 ValidationFlags 属性。

I can't quite make out whether you are attempting a generic validate-against-any-referenced-schema, or if you have a specific schema that you validate against every time, and are just not sure how to handle the references.

If it's the latter, then make the schema public on the internet, and tell people to reference it by URI.

If it's the former, then I would suggest the following:

  • First the user uploads an XML file.
  • Parse the XML file for a schema reference. Tell them "References to yourSchema.xsd were found; please upload this file below" with a new upload box.
  • Then, validate the file against the uploaded schema. To do this, modify the Schemas property of your settings object, instead of modifying the ValidationFlags property.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文