Urik
Urik

Reputation: 1698

How to get changes in XSD file affect the related CS file?

I've created an .xsd file and then used XSD.exe to generate the .cs file (I've used xsd.exe /dataset mySchema.xsd). All is working well but suddenly, I need to add a new field to my xsd.

Do I have to remove the .xsd file from the project, generate and new .cs file using XSD.exe and then add it again or is there an automatic way for VS2010 to update the .cs when I do the change to the xsd through the schema explorer?

Thanks!

Upvotes: 1

Views: 7561

Answers (2)

k3b
k3b

Reputation: 14755

The simples solution with your vs2010 project is to include your mySchema.xsd-file as a dataset into the project. If you change the xsd via code- or xsd-editor vs2010 will regenerate regenerate the cs dataset if dataset is set up correctly.

The simplest way to try this out is using the wizzard to add a new dataset.

(note i havent tried this with vs 2010 but it worked perfectly with vs2002, vs2003, vs2005 and vs2008)

[update] It works with vs2010 if xsd-file-property customtool = 'MSDataSetGenerator' is set

Upvotes: 3

Davio
Davio

Reputation: 4737

I've added a Pre-build event with a line like this:

"$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\@InstallationFolder)bin\NETFX 4.0 Tools\xsd.exe" "$(ProjectDir)MyFile.xsd"

This generates a .cs file every time (before) the project is built. It's a hassle, but it works. And if I'm right about this, the project isn't rebuilt if no changes are made to it.

Xsd2Code wasn't an option for me as I needed many xsd's to be handled and xsd.exe allows me to do this. However, you do end up with 1 giant .cs file so it may be necessary to extract any common types so they're not created twice.

If I didn't do that, it would create the same type with a slightly different name, for instance 'Signature', 'Signature1', 'Signature2', etc...

Upvotes: 0

Related Questions