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

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

问题描述

我写解析XML文件的应用程序。我有我使用尝试反序列化之前验证XML的模式(.xsd)文件:

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 /模式/ 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架构清单?有4的 settings.Schemas.Add 的,但他们没有采取嵌入资源作为参数。他们都走路径模式文件。

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.

我已经使用嵌入的资源在过去的动态设置标签的图片让我有些熟悉如何使用嵌入的资源。看看我的其他代码它看起来像什么,我终于结了是的的包含的内容:

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?

推荐答案

您可以使用添加()重载需要一个的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天全站免登陆