Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

.net - Generate DataContract from XSD

I want to be able to generate a DataContract from a XSD file, preferably using the xsd.exe tool. What is the easiest way for it to auto generate the [DataContract] and [DataMember] on each of my items?

Or is there a better approach? I am trying to avoid having to recreate the data contract each time the XSD file is changed and regenerated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The xsd.exe tool predates WCF and doesn't know anything about [DataContract] and [DataMember]. If you do use xsd.exe, you'll have to switch WCF to use the XmlSerializer instead of its default DataContractSerializer for serializing the data contracts.

The WCF equivalent for xsd.exe is svcutil.exe - it has a parameter /dconly which creates the data contracts only, from a given XSD file. This will generate a C# or VB.NET file for you, containing the data contracts nicely annotated.

Usage:

svcutil.exe (name of your XSD).xsd /dconly

This would generate a *.cs file by the same base name in your directory.

In my experience, svcutil.exe is quite picky about its XML structures - so don't be surprised if it barks at you with tons of warnings and/or errors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...