Adding (Embedded Resource) Schema To XmlReaderSettings Instead Of Filename?(将(嵌入式资源)架构添加到 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 而不是文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将(嵌入式资源)架构添加到 XmlReaderSettings 而不是
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01