在 ASP.Net 中上传引用 XSD 的 XML 文件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是我用本地模式验证 xml 的一段代码:
Here is a chunk of code I use to validate xml with a local schema:
我不太清楚您是否正在尝试通用的针对任何引用模式进行验证,或者您是否有一个每次验证的特定模式,并且只是不确定如何处理引用。
如果是后者,则将架构在互联网上公开,并告诉人们通过 URI 引用它。
如果是前者,那么我建议如下:
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:
yourSchema.xsd
were found; please upload this file below" with a new upload box.Schemas
property of yoursettings
object, instead of modifying theValidationFlags
property.