`
ladymaidu
  • 浏览: 679545 次
文章分类
社区版块
存档分类
最新评论

xsd生成实体类相关问题

 
阅读更多

1.在xsd文件中可能存在相互嵌套的情况,这是xsd就无能无力了。

http://geekswithblogs.net/mnf/archive/2008/07/22/generate-c-class-from-xsd-file.aspx

| Home |"Login failed for user" may mean "database name is invalid" >>

Generate C# class from XSD file.

I have an existing C# file, generated a long time ago from XSD definition.

The XSD files were changed, and proxy class should be regenerated.

I've tried to use XML Schema Definition Tool (Xsd.exe) to generate C# class, but it returned the error: "The datatype is missing".
I've actually have two XSD files -outer and imported

Thanks to the post: XSD.exe is kicking my butt, man...redux , it pointed me to specify all necessary files on the commandline.

Note that name of generated file is combined from the files listed

I've created the batch file to be able to rerun it

CallXSD.BAT

@call "C:/Program Files/Microsoft Visual Studio 8/VC/vcvarsall.bat" x86

xsd.exe outer.xsd imported.xsd /classes /l:cs /n:MyNamespace

rem rename outer_imported.cs as outer.cs

pause

By some reason the generated file created in some non-default encoding, which inserts 3 characters

 in the beginning.It is a minor annoyance, but when I delete them, VS shows me a warning, that wrong encoding could prevent keeping history.

The next issue was that the new XSD(.Net Framework 2.0 and 3.5) generates C# classes differently with what 1.0/1.1 XSD.exe did.

In a few places 1.1 version generated custom collection of objects, but new XSD.EXE generates array of objects, so I have to change the calls to generated classes.
Another problem with XSD generated classes is that it disables step into debugging even on methods that you extended in partial classes by specifyingDebuggerStepThroughAttribute for classes. Workarounds are described inXSD.exe and DebuggerStepThrough post. Issue reported to MS Feedback.

I should try An XSD to .NET language code class generator that adds real punch to Microsoft's XSD Tool.
or Sample Code Generator 1.4.2.1

Another issue was that authors of XSD file added xsd:choice elements and it creates extra ItemsChoiceType array and enum.I described how I addressed it inmy post XmlChoiceCollection class to access XSD generated properties for choice XML elements

That's pity, that minor changes in XSD file causes different class properties to be generated and essential code changes to access the new properties.

http://www.hanselman.com/blog/XSDexeIsKickingMyButtManredux.aspx

Decomposing XSD and WSDL into abstract definitions of types and messages is a good thing, but it appears that Microsoft’s tools don’t adequately support embracing this approach? Or, I'm completely sans clue. For example, XSD.exe doesn’t seem to be able to cope with xs:import elements when generating wrapper classes. WSDL.exe has the same problem.

Anyone have any thoughts on this? I'd like NOT to wait until Whidbey to be able to describe my messages and contracts and generate a schtickel of code. Updated with Note: Rant retracted...for now! See below ;)

Here’s a very trivial example straight from Dare's great MSDN Article.

Import.xsd:
<?xml version="1.0" encoding="utf-8" ?>
<schema xmlns:xs="&lt;a" href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema <br>targetNamespace="http://www.import.org" elementFormDefault="qualified"&gt; <br><complextype name="rootType"><br><sequence><br><element name="child1" type="xs:string" maxoccurs="2"></element><br><element name="child2" type="xs:string"></element><br></sequence><br></complextype><br></schema>

The Outer xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns:xs="&lt;a href=" http:>http://www.w3.org/2001/XMLSchema" <br> targetNamespace="<a href="http://www.example.org/">http://www.example.org</a>" <br> xmlns:tns="<a href="http://www.example.org/">http://www.example.org</a>" <br> xmlns:imp="<a href="http://www.import.org/">http://www.import.org</a>"&gt; <br><import namespace="&lt;a href=" http:>http://www.import.org" schemaLocation="<a href="file:///C:/import.xsd%22/">file:///c:/import.xsd"/</a>&gt; <br><element name="root" type="imp:rootType"></element><br></import></schema>

This schema appears to be valid – in fact, the .NET framework agrees. But when you try and run the outer schema through xsd.exe, you get the following error:

C:/>xsd /c root.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 1.1.4322.573]
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
Schema validation warning: Type 'http://www.import.org:rootType' is not declared. An error occurred at file:///C:/root.xsd, (10, 3).

Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

Error: Error generating classes for schema 'root'.
- The datatype 'http://www.import.org:rootType' is missing.

Update: Tomas was kind enough to point out not only that I had originally posted the same schema TWICE, doh! But also, more importantly, that XSD.EXE doesn't resolve schemaLocation on imports or includes, presuambly because the W3C Schema spec describes the schemaLocation attribute as a hint, not a true location.

I'm back in business as I ran XSD.EXE again with all necessary files specified on the commandline, in order to give XSD.EXE a choice of namespaces - as opposed to hoping it would somehow glean my intent!

I'm too used to using XMLSpy, XSLT and other tools for generating CS code. Good stuff, thanks Tomas! Not only was I without a clue, I was also sin una pista, sans indice - Ich war ohne einen Anhaltspunkt. That'll teach me not to post a rant in haste!

http://msdn.microsoft.com/en-us/library/ms950721

Q: Why doesn't XSD.exe support the schemaLocation attribute on imports and includes?

A: The W3C XML Schema recommendation describes this attribute as a hint, which can be ignored by processors that can use alternate means to locate schemas. XSD.exe only uses schemas that are specified through the command line to convert schema A.xsd, which imports schema B.xsd.

xsd.exe /c A.xsd B.xsd 

Also, the wsdl.exe application, considered a sister application to xsd.exe, can be downloaded from the Web. If you do this and use the wsdl.exe, you would follow schemaLocation hints in imports and includes.

google得到虾下面的工具xsd2code,安装完成,但是vs中并没有出现这个插件,不知问什么,暂时还没哟解决。

分享到:
评论

相关推荐

    通过xsd产生C#实体类解析XML或生成XML

    通过xsd产生C#实体类解析XML或生成XML,有疑问请私聊

    根据xsd批量生成java类

    但是有时候xsd生成的java类中含有汉字,结果总是有些问题。 可是xjc命令参数又没有encoding参数之类的。在网上搜了一通,忽然发现了一个好东东:xjc是由com.sun.tools.internal.xjc.Driver实现的,因此 批处理命令...

    trang工具xml自动生成实体类,xds文件自动生成实体类

    将trang.jar和要解析的xml放在同一目录,在当前文件下执行如下命令,其中users.xsd为要生成的xsd文件名 java -jar trang.jar users.xml users.xsd 执行完上述命令后会在当前文件生成users.xsd,然后执行如下命令,...

    XSD实体装换工具

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到 XML实例...

    .NET框架中的XML:在.NET框架中使用XML架构执行代码生成

    火龙果软件工程技术中心本页内容简介基础类扩展XSD处理XmlSerializer的内部原理通过CodeDom自定义映射技巧小结简介自动代码生成—无论是数据访问层、业务实体类还是用户界面—可以大大提高开发人员的生产效率。...

    wssecurity:wssecurity 示例存储库

    .wsdl 生成实体定义 Web 服务实现类配置 cxf jaxws 端点参考添加spring web配置创建 Web 应用程序 web.xml 描述符将 jetty maven 插件添加到启动实现 Web 服务客户端使用拦截器保护 Web 服务使用 WS-Policy 保护 Web...

    asp.net知识库

    直接从SQL语句问题贴子数据建表并生成建表语句的存储过程 从SQL中的一个表中导出HTML文件表格 获取數据库表的前N条记录 几段SQL Server语句和存储过程 生成表中的数据的脚本 最详细的SQL注入相关的命令整理 Oracle ...

    RapidWebDev框架源码

    常用组件是一些常用的组件和可复用API的集合,它包含了公共方法、异常处理,缓存、验证、全球化\本地化、动态类生成器、Data Context工厂、日志处理、事物处理、Spring.NET整合等等 扩展模型 扩展模型可以让你在...

Global site tag (gtag.js) - Google Analytics