将 XSD 文件转换为 C# Serializable 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1060017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:15:13  来源:igfitidea点击:

Convert XSD files to C# Serializable Class

c#xmlxsd

提问by Adam Haile

I tried following the answer to this question, but could not get xsd.exe to happily take the XSD files and turn them into a class.

我尝试按照这个问题的答案进行操作,但无法让 xsd.exe 愉快地获取 XSD 文件并将它们转换为一个类。

Using the XSD files here: http://download.adamhaile.com/SO/XSD.zip
Can anyone help me convert these to a valid C# class that can then be used to serialize an XML document to?

在此处使用 XSD 文件:http: //download.adamhaile.com/SO/XSD.zip
谁能帮我将这些文件转换为有效的 C# 类,然后可用于将 XML 文档序列化为?

Note: Yes, these are from an undocumented Yahoo Movies API that I'm trying to use. It looks like it's using a standard Microsoft based schema pattern, so I would imagine this is quite possible.

注意:是的,这些来自我正在尝试使用的未记录的 Yahoo Movies API。看起来它使用的是标准的基于 Microsoft 的架构模式,所以我认为这是很有可能的。

Here is an example of the results from one of the API calls: http://new.api.movies.yahoo.com/v2/listTheatersByPostalCode?pcode=12345&count=30&yprop=msapi

以下是其中一个 API 调用的结果示例:http: //new.api.movi​​es.yahoo.com/v2/listTheatersByPostalCode?pcode=12345&count=30&yprop=msapi

http://download.adamhaile.com/SO/XSD.zip

http://download.adamhaile.com/SO/XSD.zip

采纳答案by Cheeso

Be sure to put all referenced schemas on the cmd line.
When I do this, I get a bunch of warnings.

确保将所有引用的模式放在 cmd 行上。
当我这样做时,我会收到一堆警告。

$ xsd /c listTheatersByPostalCode.xsd yahooMovie.xsd yahooMovieCredit.xsd yahooMovieMedia.xsd yahooMoviePhoto.xsd yahooMovieTheater.xsd yahooMovieTheaterAmenity.xsd yahooMultimedia.xsd yahooUser.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The global element 'urn:yahoo:movie:theater:TheaterList' has already been declared. Line 6, position 4.
Schema validation warning: The global attribute 'urn:yahoo:movie:theater:id' has already been declared. Line 7, position 4.
Schema validation warning: The complexType 'urn:yahoo:movie:theater:TheaterListType' has already been declared. Line 10, position 4.
Schema validation warning: The complexType 'urn:yahoo:movie:theater:TheaterType' has already been declared. Line 19, position 4.
Schema validation warning: The complexType 'urn:yahoo:movie:theater:PostalAddressType' has already been declared. Line 32, position 4.
Schema validation warning: The complexType 'urn:yahoo:movie:theater:AmenityListType' has already been declared. Line 55, position 4.
Schema validation warning: The complexType 'urn:yahoo:movie:theater:MovieListType' has already been declared. Line 65, position 4.
Schema validation warning: The complexType 'urn:yahoo:movie:theater:MovieType' has already been declared. Line 71, position 4.
Schema validation warning: The complexType 'urn:yahoo:movie:theater:ShowsType' has already been declared. Line 82, position 4.

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

If I yank out yahooTheater.xsd from that list, it works fine.

如果我从该列表中删除 yahooTheater.xsd,它就可以正常工作。

I didn't even look at the XSDs but it seems to me that xsd.exe thinks some elements have been doubly defined. you may be able to manually reconcile that problem by combining yahootheater.xsd with yahooTheaterAmenity.xsd. Or, if you don't care about the amenity part, drop it.

我什至没有看 XSD,但在我看来 xsd.exe 认为某些元素已被双重定义。您可以通过将 yahooTheater.xsd 与 yahooTheaterAmenity.xsd 结合来手动协调该问题。或者,如果您不关心舒适性部分,请放弃它。

回答by nos

Use the xsd.exe that comes with visual studio. iirc xsd /classes theschema.xsd

使用visual studio 自带的xsd.exe。iirc xsd /classes theschema.xsd

But since you tried that, what went wrong ?

但既然你试过了,出了什么问题?

回答by Bob

How about giving CodeXSa try? They normally work pretty well for me.

试试CodeXS怎么样?他们通常对我来说效果很好。

回答by Alao

Try out http://www.altova.com/download.html. It has a 30 day trial but it will convert xml xsd to classes. it also reverse engineers xml to make a xsd.

试试http://www.altova.com/download.html。它有 30 天的试用期,但它会将 xml xsd 转换为类。它还对xml进行逆向工程以制作xsd。