本文介绍了安装.net framework srvice pa后出现xsd codegen问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了优秀的MSDN文章.NET Framework中的代码生成

使用XML Schema [1]构建有限xsd.exe的替代品。我的代码

在.NET framework 1.1下工作正常,但是在安装.net框架之后

service pack 1,我的代码断了。


以下是显示问题的简单示例文件:


XSD文件aaa.xsd

------------- ---------

<?xml version =" 1.0" encoding =" UTF-8"?>


< schema targetNamespace =" urn:ietf:params:xml:ns:aaa"

xmlns:aaa =" urn:ietf:params:xml:ns:aaa"

xmlns =" http://www.w3.org/2001/XMLSchema"

elementFormDefault =" qualified">


< complexType name =" aaa">

< / complexType>


< / schema>


XSD FILE bbb.xsd

------------ ----------

<?xml version =" 1.0" encoding =" UTF-8"?>


< schema targetNamespace =" urn:ietf:params:xml:ns:bbb"

xmlns:bbb =" urn:ietf:params:xml:ns:bbb"

xmlns:aaa =" urn:ietf:params:xml:ns:aaa"

xmlns =" http://www.w3.org/2001/XMLSchema"

elementFormDefault =" qualified">


< import namespace =" urn:ietf:params:xml:ns:aaa"

schemaLocation =" aaa.xsd" />


< element name =" test" />


< / schema>

这是我用来处理上述模式文件的代码(从上面提到的MSDN文章[1]中获取了几乎排队的行,但是增加了读取多个xsd文件的功能:

):


xsdCodeGen.cs

----------------------

string [ ] xsdFiles = {" aaa.xsd"," bbb.xsd" };

XmlSchemas架构=新的XmlSchemas();


//加载XmlSchema及其集合

foreach(string filename在xsdFiles中)

{

XmlSchema xsd;

使用(FileStream fs = new FileStream(filename,FileMode.Open,

FileAccess.Read))

{

xsd = XmlSchema.Read(fs,null);

xsd.Compile(null) ;

}

schemas.Add(xsd);

}


//创建这些模式的导入器

XmlSchemaImporter importer = new XmlSchemaImporter(模式);

// XmlCodeExporter的System.CodeDom命名空间将类放入

CodeNamespace ns = new CodeNamespace(" testNamespace");

XmlCodeExporter exporter = new XmlCodeExporter(ns);


//迭代架构top -level元素和导出代码每个

foreach(模式中的XmlSchema xsd)

{

foreach(xsd.Elements.V中的XmlSchemaElement元素) alues)

{

//导入映射,并导出代码

XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);

exporter.ExportTypeMapping(mapping);

}

}

以上代码可以正常使用pre-service pack,但是post-service打包行:


XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);


将抛出以下异常:


例外:数据类型''urn:ietf:params:xml:ns:bbb:aaa''缺失。

任何想法为什么不同的行为售前和售后服务包以及

如何解决?


非常感谢提前,


Ceri Williams


[1]

I followed the excellent MSDN article "Code Generation in the .NET Framework
Using XML Schema" [1] to build a substitute for the limited xsd.exe. My code
works fine under .NET framework 1.1, but after installing .net framework
service pack 1, my code breaks.

Below are trivial example files that demonstrate the problem:

XSD FILE aaa.xsd
----------------------
<?xml version="1.0" encoding="UTF-8"?>

<schema targetNamespace="urn:ietf:params:xml:ns:aaa"
xmlns:aaa="urn:ietf:params:xml:ns:aaa"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<complexType name="aaa">
</complexType>

</schema>

XSD FILE bbb.xsd
----------------------
<?xml version="1.0" encoding="UTF-8"?>

<schema targetNamespace="urn:ietf:params:xml:ns:bbb"
xmlns:bbb="urn:ietf:params:xml:ns:bbb"
xmlns:aaa="urn:ietf:params:xml:ns:aaa"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<import namespace="urn:ietf:params:xml:ns:aaa"
schemaLocation="aaa.xsd"/>

<element name="test"/>

</schema>
And here is the code which I use to process the above schema files (taken
almost line for line from the MSDN article mentioned above [1], but with the
addition of the ability to read multiple xsd files):

xsdCodeGen.cs
----------------------
string[] xsdFiles = { "aaa.xsd", "bbb.xsd" };
XmlSchemas schemas = new XmlSchemas();

// Load the XmlSchema and its collection
foreach(string filename in xsdFiles)
{
XmlSchema xsd;
using(FileStream fs = new FileStream(filename, FileMode.Open,
FileAccess.Read))
{
xsd = XmlSchema.Read(fs, null);
xsd.Compile(null);
}
schemas.Add(xsd);
}

// Create the importer for these schemas
XmlSchemaImporter importer = new XmlSchemaImporter(schemas);

// System.CodeDom namespace for the XmlCodeExporter to put classes in
CodeNamespace ns = new CodeNamespace("testNamespace");
XmlCodeExporter exporter = new XmlCodeExporter(ns);

// Iterate schema top-level elements and export code for each
foreach(XmlSchema xsd in schemas)
{
foreach (XmlSchemaElement element in xsd.Elements.Values)
{
// Import the mapping, and export the code
XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);
exporter.ExportTypeMapping(mapping);
}
}
The above code works fine pre- service pack, but post- service pack the line:

XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);

will throw the following exception:

Exception: The datatype ''urn:ietf:params:xml:ns:bbb:aaa'' is missing.
Any ideas why the different behaviour pre- and post- service pack, and how
to fix?

Many thanks in advance,

Ceri Williams

[1]
http://msdn.microsoft.com/XML/Buildi...xsdcodegen.asp

推荐答案





这篇关于安装.net framework srvice pa后出现xsd codegen问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:02