将(嵌入式资源)架构添加到 XmlReaderSettings 而不是文件名? [英] Adding (Embedded Resource) Schema To XmlReaderSettings Instead Of Filename?

查看:23
本文介绍了将(嵌入式资源)架构添加到 XmlReaderSettings 而不是文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个解析 Xml 文件的应用程序.我有架构 (.xsd) 文件,用于在尝试反序列化之前验证 Xml:

I am writing an application that parses an Xml file. I have the schema (.xsd) file which I use to validate the Xml before trying to deserialize it:

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, "./xml/schemas/myschema.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(xmlFile, settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(settings_ValidationEventHandler);
document.Validate(eventHandler);

请注意,参数 *./xml/schemas/myschema.xsd" 是 .xsd 相对于程序执行的路径.

Note that the parameter *./xml/schemas/myschema.xsd" is the path to the .xsd relative to program execution.

我不想使用文件名/路径,而是将 .xsd 文件编译为我的项目中的嵌入式资源(我已经添加了 .xsd 文件并将构建操作设置为嵌入式资源).

I don't want to use filenames/paths, instead I would rather compile the .xsd file as an embedded resource in my project (I have already added the .xsd file and set the Build Action to Embedded Resource).

我的问题是....如何将嵌入式资源架构添加到 XmlReaderSettings 架构列表?settings.Schemas.Add 有 4 个重载方法,但没有一个将嵌入资源作为参数.它们都采用架构文件的路径.

My question is.... how do I add the Embedded Resource schema to the XmlReaderSettings schema list? There are 4 overloaded methods for settings.Schemas.Add but none of them take an embedded resource as an argument. They all take the path to the schema file.

我过去使用嵌入式资源来动态设置标签图像,因此我对使用嵌入式资源有些熟悉.查看我的其他代码,看起来我最终得到的是一个包含内容的 Stream:

I have used embedded resources in the past for dynamically setting label images so I am somewhat familiar with using embedded resources. Looking at my other code it looks like what I eventually end up with is a Stream that contains the content:

System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream(resourceName);

我假设嵌入的 .xsd 也将作为流读入,所以这稍微缩小了我的问题.当我引用包含架构而不是文件名的流时,如何将架构添加到 XmlReaderSettings?

I am assuming that the embedded .xsd will also be read in as a stream so this narrows down my question a bit. How do I add the schema to XmlReaderSettings when I have a reference to the stream containing the schema and not the filename?

推荐答案

您可以使用 Add() 重载,该重载将 XmlReader 作为其第二个参数:

You can use the Add() overload that takes an XmlReader as its second parameter:

Assembly myAssembly = Assembly.GetExecutingAssembly();
using (Stream schemaStream = myAssembly.GetManifestResourceStream(resourceName)) {
  using (XmlReader schemaReader = XmlReader.Create(schemaStream)) {
    settings.Schemas.Add(null, schemaReader);
  }
}

或者您可以先加载架构,然后添加它:

Or you can load the schema first and then add it:

Assembly myAssembly = Assembly.GetExecutingAssembly();
using (Stream schemaStream = myAssembly.GetManifestResourceStream(resourceName)) {
  XmlSchema schema = XmlSchema.Read(schemaStream, null);
  settings.Schemas.Add(schema);
}

这篇关于将(嵌入式资源)架构添加到 XmlReaderSettings 而不是文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆