Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsd.exe - schema to class - for use with WCF

Tags:

c#

wcf

schema

xsd

I have created a schema as an agreed upon interface between our company and an external company. I am now creating a WCF C# web service to handle the interface.

I ran the XSD utility and it created a C# class. The schema was built in BizTalk, and references other schemas, so all-in-all there are over 15 classes being generated.

I put [DataContract} attribute in front of each of the classes. Do I have to put the [DataMember] attribute on every single property?
When I generate a test client program, the proxy does not have any code for any of these 15 classes.

We used to use this technique when using .asmx services, but not sure if it will work the same with WCF. If we change the schema, we would want to regenerate the WCF class, and then we would haev to each time redecorate it with all the [DataMember] attributes? Is there an newer tool similar to XSD.exe that will work better with WCF?

Thanks,

Neal Walters

SOLUTION (buried in one of Saunders answer/comments):

Add the XmlSerializerFormat to the Interface definition:

    [OperationContract]
    [XmlSerializerFormat]     // ADD THIS LINE 
    Transaction SubmitTransaction(Transaction transactionIn);

Two notes: 1) After I did this, I saw a lot more .xsds in the my proxy (Service Reference) test client program, but I didn't see the new classes in my intellisense. 2) For some reason, until I did a build on the project, I didn't get all the classes in the intellisense (not sure why).

like image 310
NealWalters Avatar asked Mar 06 '10 00:03

NealWalters


People also ask

What is XSD EXE?

The XML Schema Definition (Xsd.exe) tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.

How do I import XSD files into Visual Studio?

You can easily just add an existing XSD on disk to your Visual Studio project by doing a "Add Existing Item" and then picking that file. There's no separate "import / export" functionality, really.

Why XSD is used in C#?

XSD is a schema language; you use it to define the possible structure and contents of an XML format. A validating parser can then check whether an XML instance document conforms to an XSD schema or a set of schemas.


1 Answers

Neal, besides all the options John has given you, you should also check out the WCSF.blue tool on Codeplex: http://wscfblue.codeplex.com/

It's a "contract first" approach to doing WCF, and one of the many options it offers is to create a WCF DataContract file from your XSD:

alt text

This then pops up a dialog in which you can set a slew of parameters on how to create your C# class file from the XSD:

alt text

Quite useful, even if you want to use it for nothing more than converting XSD to C# classes that work as WCF DataContracts :-)

Also see this blog post for more explanations on the XSD DataContract generation process.

like image 121
marc_s Avatar answered Oct 04 '22 04:10

marc_s