处理xsd:将XSD包含在Visual Studio中作为嵌入式资源

时间:2013-08-16 13:26:11

标签: c# .net visual-studio-2012 xsd embedded-resource

我有一组XSD可以针对XMLSPY和Java代码进行验证。我需要将这组XSD作为Visual Studio 2012 .net中的嵌入式资源。不幸的是,当尝试使用自定义XmlResolver解决它们以处理xsd:include时,我收到一个错误,即已声明了全局元素。错误很奇怪,因为元素只被声明一次。

Visual Studio Solution
    |-----------    Visual Studio Project
            |-----------    Schemas (Embedded Resource)
                    |-----------    Directory A
                            |------------ set of XSDs that are referenced by XSDs in Directory B and to a globaltype definition file located in this directory
                    |-----------    Directory B
                            |------------- set of XSDs that reference each other and those in Directory A, the XSD call from the main is located here

验证Util类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace ABC.XYZ.Utils
{
    public static class XmlUtil
    {
        private static bool isValid;

        public static bool ValidateXml(string targetNamespace, string schemaUri, string xml)
        {
            isValid = true;

            var schemaReaderSettings = new XmlReaderSettings() { ValidationType = ValidationType.Schema };
            schemaReaderSettings.ValidationEventHandler += MyValidationHandler;

            schemaReaderSettings.Schemas.XmlResolver = new XmlResourceResolver();
            var schemaReader = XmlReader.Create(GetSchemaStream(schemaUri), schemaReaderSettings);
            schemaReaderSettings.Schemas.Add(targetNamespace, schemaReader);

            var x = XElement.Parse(xml);
            var sr = new System.IO.StringReader(x.ToString());

            XmlReader validatingReader = XmlReader.Create(sr, schemaReaderSettings);

            while (validatingReader.Read())
            {
            }

            validatingReader.Close();

            return isValid;
        }

        private static void MyValidationHandler(object sender, ValidationEventArgs args)
        {
            Console.WriteLine("***Validation error");
            Console.WriteLine("\tSeverity:{0}", args.Severity);
            Console.WriteLine("\tMessage:{0}", args.Message);
            isValid = false;
        }

        private static Stream GetSchemaStream(string relativeFileName)
        {
            var resourceFileName =
                Assembly.GetExecutingAssembly()
                    .GetManifestResourceNames()
                    .FirstOrDefault(p => p.EndsWith(relativeFileName));
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceFileName);
        }

    }
}

自定义XmlResolver

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ABC.XYZ.Utils
{
    public class XmlResourceResolver : XmlResolver
    {
        public const string AssemblyDefaultNamespace = "ABC.XYZ";
        public const string SchemasNamespace = "Schemas";

        public override Uri ResolveUri(Uri baseUri, string relativeUri)
        {
            var result = new UriBuilder("res://", AssemblyDefaultNamespace, -1, SchemasNamespace.Replace(".", "/"));
            result.Path += "/" + relativeUri.Replace("../", "/").TrimStart('/');
            return result.Uri;
        }

        public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
        {
            if (absoluteUri.Scheme != "res") return null;
            Debug.WriteLine("Loading resource based on location {0}", absoluteUri);

            var assembly = Assembly.GetExecutingAssembly();
            var name = String.Format(CultureInfo.InvariantCulture, "{0}{1}",
                absoluteUri.Host,
                absoluteUri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped).Replace("/", "."));

            // try for an exact match based on schemaLocation hint path
            var resourceName = (from x in assembly.GetManifestResourceNames()
                                where name.Equals(x, StringComparison.OrdinalIgnoreCase)
                                select x).FirstOrDefault();

            // if not match based on filename alone
            if (resourceName == null)
            {
                var schemaDocumentName = Path.GetFileName(absoluteUri.AbsolutePath);
                Debug.WriteLine("Unable to locate exact match, looking for match based on filename {0}", schemaDocumentName);
                resourceName = (from x in assembly.GetManifestResourceNames()
                                where x.Contains(SchemasNamespace) && 
                                      x.EndsWith("." + schemaDocumentName, StringComparison.OrdinalIgnoreCase)
                                select x).FirstOrDefault();
            }

            Debug.WriteLine("Loading resource {0}", resourceName);
            var stream = assembly.GetManifestResourceStream(resourceName);
            return stream;
        }
    }

}

非常感谢对此问题的任何见解。

2 个答案:

答案 0 :(得分:0)

XSD 1.0鼓励但不要求验证程序检测同一模式文档的多个包含(或导入)并仅包含它们一次。

结果是,从多个其他模式文档中多次包含相同的模式文档是使用XSD创建互操作性噩梦的最简单方法。 (不是唯一的方法,只是最简单的方法。)如果您拥有架构文档,请将导入的所有包含和所有架构位置信息隔离到驱动程序文件中,并从“正常”中删除所有包含和所有架构位置提示'架构文件。

答案 1 :(得分:0)

问题是当你使用XMLSpy时,XSD是文件系统中的文件;然后发生了什么,对于每个文件都有一个基本URI,因此解析器将使用该信息来确保一旦加载XSD,基于URI比较就不再加载相同的文件。

现在,通过从程序集加载流来实现它的方式,所有信息都消失了(您的流没有基本URI)。你的解析器将不断地从不同的地方反复加载相同的XSD,从而产生这种冲突。

我所知道的所有XSD处理器都没有采用任何其他方法来过滤相同XSD内容的多个包含,而是基源URI。

在.NET中,最简单的方法可能是(再次,取决于图表的复杂程度)来尝试this post中的解决方案;整个想法是提供一个基本URI,它应该提供避免多个包含所需的信息。

另一种选择可能是在您的自定义解析器中确保对于您正在解析的任何给定URI,您只返回一次流(在所有其他情况下返回null)。只要您没有使用xsd:redefine组合,就可以保证这一点(在这种情况下,解决方案是对模式文件图形进行拓扑排序并确保首先加载所有xsd:redefines)。

对于@CMSperbergMcQueen点,保证可行的方法是重构所有XSD,使每个命名空间只有一个XSD嵌入资源;每个XSD都会删除所有导入(技术称为“悬空”)。将这些XSD添加到XML Schema集作为独立的XSD并进行编译。除非您遇到.NET错误,否则结果应该是已编译的XmlSchemaSet。