当我的所有.XSD存储为资源时,如何解析.XSD的schemaLocation属性?
我正在开发一个项目,我需要根据嵌套的XSD生成XML文件。 例如,ORDER引用了PERSON,PERSON引用了ADDRESS等。
我正在创建一个`XmlReaderSettings’实例来validationXSD,并在生成后validationXML。
我已将XSD作为资源添加到我的程序集中。 然后,我为每个资源创建一个XmlSchema
实例,从最低到最高,并将其添加到XmlReaderSettings.Schemas
集合。
但是,尝试添加引用另一个架构的架构时失败。 我得到一个XmlSchemaException:“对于元素声明,必须存在name或ref属性。”
我在下面提供了示例XSD和源代码:
ADDRESS.xsd – 由PERSON.xsd引用
PERSON.xsd
示例代码 – 加载并validationXSD
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Schema; using SchemaTest.Properties; namespace SchemaTest { class Program { static void Main(string[] args) { // create validation settings instance var xmlReaderSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema, ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation | XmlSchemaValidationFlags.ReportValidationWarnings }; xmlReaderSettings.ValidationEventHandler += SchemaValidationEventHandler; // added XmlResourceResolver, per the accepted answer below xMLReaderSettings.Schemas.XmlResolver = new XmlResourceResolver(); // load schemas into validation settings instance LoadSchema(xmlReaderSettings, Resources.PERSON); // validate schemas xmlReaderSettings.Schemas.Compile(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } private static void LoadSchema(XmlReaderSettings xmlReaderSettings, string schemaString) { using (var schemaStream = new MemoryStream(Encoding.Default.GetBytes(schemaString))) { var schema = XmlSchema.Read(schemaStream, null); try { xmlReaderSettings.Schemas.Add(schema); } catch (Exception ex) { Console.WriteLine("EXCEPTION: {0}", ex.Message); } } } private static void SchemaValidationEventHandler(object sender, ValidationEventArgs e) { Console.WriteLine("{0}: {1}", e.Severity, e.Message); } } }
我不确定这是否与ValidationFlags
,但我尝试删除XmlSchemaValidationFlags.ProcessSchemaLocation
并仍然收到相同的错误。 我还尝试将SchemaValidationEventHandler
传递给XmlSchema.Read
方法(而不是示例代码中的null),并且似乎创建了模式,但是在将attemtping添加到XmlReaderSettings.Schemas
集合时它仍会引发exception。
编辑 – 2011.11.03
我根据下面接受的答案中的XmlResolver
类示例创建了我自己的XmlUrlResolver
后代,称为XmlResourceResolver
,从而解决了这个问题(双关语)。
要消除错误,请在PERSON.xsd文件中将type =“Addresses”更改为name =“Addresses”。
最好使用下面的代码来编译模式。 重要的是确保你建立一个基础uri(因为你正在从流中读取),使用XmlSchemaSet,以及自定义解析器(将文件作为嵌入资源读取)。
上述就是C#学习教程:当我的所有.XSD存储为资源时,如何解析.XSD的schemaLocation属性?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Schema; namespace ConsoleApplication3 { class Program { class XmlResolver : XmlUrlResolver { internal const string BaseUri = "schema://"; public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn) { if (absoluteUri.Scheme == "schema") { switch (absoluteUri.LocalPath) { case "/ADDRESS.xsd": return new MemoryStream(Encoding.UTF8.GetBytes(Resource.ADDRESS)); case "/PERSON.xsd": return new MemoryStream(Encoding.UTF8.GetBytes(Resource.PERSON)); } } return base.GetEntity(absoluteUri, role, ofObjectToReturn); } } static void Main(string[] args) { using (XmlReader reader = XmlReader.Create(new StringReader(Resource.PERSON), new XmlReaderSettings(), XmlResolver.BaseUri)) { XmlSchemaSet xset = RetrieveSchemaSet(reader); Console.WriteLine(xset.IsCompiled); } } private static XmlSchemaSet RetrieveSchemaSet(XmlReader reader) { XmlSchemaSet xset = new XmlSchemaSet() { XmlResolver = new XmlResolver() }; xset.Add(XmlSchema.Read(reader, null)); xset.Compile(); return xset; } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/959388.html