• <i id='dutyr'><tr id='dutyr'><dt id='dutyr'><q id='dutyr'><span id='dutyr'><b id='dutyr'><form id='dutyr'><ins id='dutyr'></ins><ul id='dutyr'></ul><sub id='dutyr'></sub></form><legend id='dutyr'></legend><bdo id='dutyr'><pre id='dutyr'><center id='dutyr'></center></pre></bdo></b><th id='dutyr'></th></span></q></dt></tr></i><div id='dutyr'><tfoot id='dutyr'></tfoot><dl id='dutyr'><fieldset id='dutyr'></fieldset></dl></div>

  • <legend id='dutyr'><style id='dutyr'><dir id='dutyr'><q id='dutyr'></q></dir></style></legend>
    <tfoot id='dutyr'></tfoot>
  • <small id='dutyr'></small><noframes id='dutyr'>

      <bdo id='dutyr'></bdo><ul id='dutyr'></ul>

        将(嵌入式资源)架构添加到 XmlReaderSettings 而不是

        时间:2023-06-02

        <small id='eACbU'></small><noframes id='eACbU'>

      1. <i id='eACbU'><tr id='eACbU'><dt id='eACbU'><q id='eACbU'><span id='eACbU'><b id='eACbU'><form id='eACbU'><ins id='eACbU'></ins><ul id='eACbU'></ul><sub id='eACbU'></sub></form><legend id='eACbU'></legend><bdo id='eACbU'><pre id='eACbU'><center id='eACbU'></center></pre></bdo></b><th id='eACbU'></th></span></q></dt></tr></i><div id='eACbU'><tfoot id='eACbU'></tfoot><dl id='eACbU'><fieldset id='eACbU'></fieldset></dl></div>
          <bdo id='eACbU'></bdo><ul id='eACbU'></ul>
          <tfoot id='eACbU'></tfoot>
        • <legend id='eACbU'><style id='eACbU'><dir id='eACbU'><q id='eACbU'></q></dir></style></legend>

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

                  问题描述

                  限时送ChatGPT账号..

                  我正在编写一个解析 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 而不是文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:文件类型的默认构建操作 下一篇:如何在 WCF 服务中使用 RequestContext 映射路径

                  相关文章

                  最新文章

                1. <i id='jmy2Q'><tr id='jmy2Q'><dt id='jmy2Q'><q id='jmy2Q'><span id='jmy2Q'><b id='jmy2Q'><form id='jmy2Q'><ins id='jmy2Q'></ins><ul id='jmy2Q'></ul><sub id='jmy2Q'></sub></form><legend id='jmy2Q'></legend><bdo id='jmy2Q'><pre id='jmy2Q'><center id='jmy2Q'></center></pre></bdo></b><th id='jmy2Q'></th></span></q></dt></tr></i><div id='jmy2Q'><tfoot id='jmy2Q'></tfoot><dl id='jmy2Q'><fieldset id='jmy2Q'></fieldset></dl></div>

                2. <small id='jmy2Q'></small><noframes id='jmy2Q'>

                    <legend id='jmy2Q'><style id='jmy2Q'><dir id='jmy2Q'><q id='jmy2Q'></q></dir></style></legend>
                      <bdo id='jmy2Q'></bdo><ul id='jmy2Q'></ul>
                    <tfoot id='jmy2Q'></tfoot>