Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

XSD.EXE and included schemas?


:P
On this page:

Here’s a basic question that I can’t seem to figure out: I need to import a schema (specifically OFX schema) into .NET and I’m trying to do this with XSD. The schema files are broken up into linked schema files (ie. other schemas are included into the ‘main’ schema file). The schemas work fine when I load them up the into Visual Studio for display. The designer properly shows the schema and all of the related included types.

 

However when I run XSD.EXE against the schema it complains that all the imported schema types are missing and ultimately fails to generate the class(es) for the schema.

 

Looking at XSD.EXE I don’t see anyway to specify the whole lot of files to import and apparently XSD.EXE doesn’t look at the includes which look something like this:

 

<xsd:schema targetNamespace="http://ofx.net/types/2003/04" xmlns:ofx="http://ofx.net/types/2003/04" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

      <xsd:include schemaLocation="OFX_Banking_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_BillPay_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_BillerDelivery_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_BillerDirectory_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_CreditCard_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Email_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Image_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_InterTransfer_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Investment_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Loan_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Profile_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_SecuritiesList_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Signon_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Signup_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Tax1098_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_Tax1099_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_TaxW2_Message_Wrappers.xsd"/>

      <xsd:include schemaLocation="OFX_WireTransfer_Message_Wrappers.xsd"/>

      <xsd:complexType name="OFXRequestType">

 

 

Anybody have any idea how I can do this?

 

Also I seem to remember that there’s some way to do the type generation from within Visual Studio without explicitly running XSD. It sure would be nice that once the schema is loaded in VS, to right click generate classes/dataset right out of the designer…

Posted in XML  

The Voices of Reason


 

Chris D
January 26, 2007

# re: XSD.EXE and included schemas?

I assume all those schema's share the same targetNamespace? Otherwise you need to use xsd:import instead of xsd:include.

Rick Strahl
January 26, 2007

# re: XSD.EXE and included schemas?

No looks like all the included schemas have the same target and main namespace settings:

Main Protocol xsd:

<xsd:schema targetNamespace="http://ofx.net/types/2003/04" xmlns:ofx="http://ofx.net/types/2003/04" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Investment Messages xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ofx="http://ofx.net/types/2003/04" targetNamespace="http://ofx.net/types/2003/04" elementFormDefault="unqualified">

I thought maybe that XSD is not findng the other files, but looking at FileMon - it's definitely opening the other schema files. So it appears it's not happy consolidating what it finds in the includes.

The OFX files have a special dotnet version for the main xsd file but from what I can glean from it the only thing that does if fix up the array/collection implementation so that .NET can properly create those types which should not be an issue for the child objects. Also doesn't work with the other non dotnet specific schemas.

In case anybody's looked at this the files come from the ofx main site here:
http://www.ofx.net/ofx/de_spec.asp

Steven Berkovitz
January 26, 2007

# re: XSD.EXE and included schemas?

This was something that was changed from VS2003 to 2005 for some unknown reason...

Pull up the File Properties on the XSD file in visual studio. Set the "Custom Tool" property to MSDataSetGenerator. Right click the file and choose "Run Custom Tool"

Magnus Hytten
January 27, 2007

# re: XSD.EXE and included schemas?

I think you need to specify all the schema-files as arguments for xsd.exe.

Something like this: xsd.exe mainschema.xsd linkedschema1.xsd linkedschema2.xsd .....

Hope that helps.

Rick Strahl
January 27, 2007

# re: XSD.EXE and included schemas?

Magnus, tried that but xsd.exe choked on that. I don't think it supports multiple files, but I can't tell by the documentation.

Magnus Hytten
January 27, 2007

# re: XSD.EXE and included schemas?

I downloaded the "OFX 2.1.1 spec and schema".

Tested: xsd.exe OFX3_Protocol_dotNET.xsd /c

and I got a .cs file out without any problems... Am I testing with the wrong xsd?

Peter Bromberg
January 27, 2007

# re: XSD.EXE and included schemas?

Rick,
Have you tried XSDObjectGen? It provides a 2005 command line executable. A couple snippets from the doc:

Support for the most popular XML schema constructs: Supports (inheritance, groups, abstract types, elements, attributes, complexType, simpleType, enumeration, any, anyType). Limited support for “interesting” schema constructs (choice, substitution groups , complex type restriction, complex type extension using regular expressions). No support for “questionable” schema constructs (union, redefine).

Multipart schema support: Some schemas reference other schemas via include constructs. The generator provides full support for schemas that span multiple files using the XML Schema include statement. Some schemas are defined by incorporating types defined in other schemas and importing them. The sample code generator reads these multi-namespace schemas and optionally generates a file per XML namespace encountered. No support is provided for a file per include, since these all create a single namespace.

Rick Strahl
January 27, 2007

# re: XSD.EXE and included schemas?

Magnus... this is really odd. I retried the code I have here and it failed immediately. I checked versions etc. and it's definitely the same exact download. So I went and downloaded again and sure enough I have no problems with this now. This is bizarre - maybe I missed a file when unpacking the zip <shrug>...

As it turns out though the _dotnet file doesn't work as it actually is a different schema that's not compatible with the stuff I need to parse against. The version 2.0.1 schema and the _classic schemas have the right data.

I managed to get some test data set up and it appears it's parsing from deserialization, but I end up with null data beyond the top level element...

I think it might just be easier to parse the document with the DOM rather than trying fit the damn class mapping to it... <s>

Richard Soeteman
January 28, 2007

# re: XSD.EXE and included schemas?

Rick,

XSD.EXE is limited, that's why I use CodeXS(Open Source). It suports multi included schema's and creates an output file per schema. It's using a plugin architecture so you can modify the output with your own codemodifiers.

Check out the url below for a full feature list and to download CodeXS

Yow-Hann
January 28, 2007

# re: XSD.EXE and included schemas?

I recommend checking out the program in question (XSD.EXE). It proves to be a very helpful utility when the documentation is scarce. If it contains a CLI header, you can take a peek and confirm its support of importing multiple xsd (entry at http://geekswithblogs.net/yowhann/archive/2007/01/28/104797.aspx).

wagnerblog.com
February 21, 2007

# wagnerblog.com &raquo; Blog Archive &raquo; OFX: Specification


Curtis Gray
May 30, 2007

# re: XSD.EXE and included schemas?

XSD.exe has behaved itself when using /dataset rather than /classes on the command line. It will correctly generate a DataSet class file where it wouldn't generate a plain-old class file. So, if you don't mind accessing your data as derivitive of System.Data.DataSet you just may have a chance to import a complete multi-file schema.

Curtis

junlia
September 27, 2007

# re: XSD.EXE and included schemas?

Rick,

Did you get a good solution with the xsd.exe's limitation on schemalocation? I tried all the suggested approaches for a schema import a ton of other schemas -- a standalone schema in STAR5.0.4, and none of them worked for me.

Did you use DOM to parse it, or using other tools?

Thanks much,
Junlia

Rizwan Raza
November 13, 2007

# re: XSD.EXE and included schemas?

I saved the imported schemas that were referenced locally without changing the import statements in the xsd file and it generated the object like a charm

Following is the command that I used

"c:\program files\microsoft visual studio 8\sdk\v2.0\bin\xsd.exe" /c /l:c# /n:<YorNamespace> EntityA.xsd Imported_01.xsd

Where Imported_01.xsd is referenced in the xsd file like below

<xs:import namespace = "http://../Imported_01/" schemaLocation = "http://../Imported_01.xsd"/>

I also tried the following but that did not work

"c:\program files\microsoft visual studio 8\sdk\v2.0\bin\xsd.exe" /c /l:c# /n:<YorNamespace> EntityA.xsd http://../Imported_01.xsd

Bob Denny
February 26, 2008

# re: XSD.EXE and included schemas?

This saved my A** big time. Thank you VERY VERY MUCH for that discovery. Ot worked like a charm, once I had copies of the imported files sitting in the same folder with the root schema. I had actually patched the import to point to the local files...

Cleydson
May 14, 2008

# re: XSD.EXE and included schemas?


Dave Black
May 22, 2008

# re: XSD.EXE and included schemas?

Be aware that using XSD.exe and XSDObjectGen.exe will yield *very* different classes. I had a project (using Google Checkout API) that worked with one but not the other. I can't remember which worked and which didn't but suffice it to say that they generate quite different results. Go figure : P

AFAIK, XSDObjectGen.exe is deprecated and MS is now pushing XSD.exe more. I don't have any hard evidence other than what I have heard thru some of my MS friends.

Ken Blackford
September 04, 2008

# re: XSD.EXE and included schemas?

I have a tool I have been using for about a year... I am not sure where I found it. After a search on the internet for the author - "Ryan Herbert", company - "CDR Technical Solutions" and the name "XSD GUI" the executable is named "xsd-gui.exe", I find nothing. i use this tool all the time. I allows for generating code using xsd from multiple files.

Maybe you can find it on the net. I would distribute it but I am unable to locate the readme for it and the license agreement to see if I can legally do so.

Hope this helps ya.

Ken

Lukash
September 11, 2008

# re: XSD.EXE and included schemas?

I had the same kind of issue so i implemented a custom tool linked to XSD thats just loop inside XSD file and retrieve all the sub xsd file imported inot the imported or included sub xsd files... and then call xsd with the list of sub xsd files :

xsd.exe sub1.xsd sub2.xsd sub3.xsd main.xsd...

Diego
February 05, 2009

# re: XSD.EXE and included schemas?

Lukash:

Can you explain in detail what is it that you did to generate the classes? I'm actually trying to use XSD.EXE to create classes for B2MML schemas. The guy in WBF who is in charge of these schemas named you and this post as the solution, but I can't seem to understand what is it that you did...

Leonardo
July 03, 2009

# re: XSD.EXE and included schemas?

I needed to generate a class for a project, i had one XSD file that imported three other XSD.
In order for xsd.exe to work i had to copy the four files into c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin and then run XSD.exe.

Rahul
November 20, 2009

# re: XSD.EXE and included schemas?

Try using SchemaLocation attribute of the import, and specify the XSD file location of imported schema. It worked for me.

Erhan Hosca
November 22, 2009

# re: XSD.EXE and included schemas?


Leles
December 13, 2009

# re: XSD.EXE and included schemas?

Hey guys!
I've been able to generate the .cs file by just giving the complete address of the dependencies, like this:
schemaLocation="file:///c:/newDS_app2.xsd"

and running XSD.exe WITHOUT the dependencies!
xsd /d /l:cs c:\newDS.xsd

The class got created, but still can't make it compatible with the IDE designer!

anonymous
August 30, 2010

# re: XSD.EXE and included schemas?

Define all includes prior to your primary xsd. Example:

c:\program files\microsoft sdks\windowsv\7.0a\bin> xsd.exe "c:\schemas\include1.xsd" "c:\schemas\include2.xsd" "c:\schemas\include3.xsd"
"c:\schemas\primary.xsd" /c /l:vb

Sumana
September 05, 2012

# re: XSD.EXE and included schemas?

Can this XSD.exe supports <redefine> tag if the XSD is extended and if not please guide me to get the solutions. This will save me from one of the biggest issue am facing. The same XSD is getting validatd with Java validator.

Am not sure what is the solution in .net. And i need to use the extended xsd to generate a class.

Thanks
Sumana

Alex Dresko
October 08, 2015

# re: XSD.EXE and included schemas?

I wrote a quick LinqPad script to achieve the desired result with XSD.EXE... http://www.alexdresko.com/2015/10/08/xsd-exe-and-imports-a-solution/

ThePukeOne
July 12, 2019

# re: XSD.EXE and included schemas?

Well, in 2019, xsd.exe still can't read import element... Microsoft


Rick Strahl
July 13, 2019

# re: XSD.EXE and included schemas?

@ThePukeOne - Translation: It'll never happen 😃


Ludovic
June 04, 2021

# re: XSD.EXE and included schemas?

If you have a lot of xsd, you should use a configuration file. For example GenerateClassFromXSD.xml :

<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateClasses language='CS' namespace='Namespace.subnamespace'>
    <schema>A2.xsd</schema>
    <schema>A21.xsd</schema>
    <schema>.\A22.xsd</schema>
</generateClasses>
</xsd>

It is very important to have the ".\" in last xsd declaration (otherwise, you will have a too big filename > 260 car.)

xsd /p:GenerateClassFromXSD.xml

See the final tips here : https://stackoverflow.com/questions/3859319/tool-that-can-combine-many-xsd-files-into-one/7000894


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024