Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
446
XSD targetNamespace
Of course, the XSD tool is also XML namespace-aware. If we specify a default
namespace in the source XML document:
<?xml version=”1.0”?>
<garage xmlns="urn:GarageSchema.xsd">
<…>
xsd.exe would make the namespace the targetNamespace of the schema:
If the source document does not contain a namespace declaration you have to
manually add the xmlns and targetNamespace attributes to the generated schema.
If the source document references content from multiple namespaces the tool
will generate a schema file for each of the referenced namespaces.
Extracting All Schema Types
The most prevalent use case for the XSD tool is to generate classes
corresponding to the complexTypes in an XML schema. XML schemas are the
starting point when we develop an application exchanging data through XML. As
soon as both parties agreed on the data exchange format each one can go off and
develop their application. XML schema is a good choice to document the format,
because of it ability to express complex XML formats and the wide spread tool
support, for the creation and validation of XSD, or, more important, it will
also enable us to create classes automatically.
We can invoke the XML Schema Definition Tool with the /c switch from the
command-line when we need to create classes for the top-level elements in the
schema:
xsd.exe garage.xsd /c /l:CS
The “.xsd” extension signals that we want to create classes from a schema, the
/c switch signals that we want stand-alone classes, not a typed DataSet. The /l
switch specifies to language to generate the classes in. Possible choices are
‘CS’ for C#, ‘VB’ for Visual Basic.NET and ‘JS’ for Jscript. You could provide
your own code emitter classes to produce classes in additional languages, but
that is beyond the scope of this chapter. With the /l:CS switch we provided,
the tool creates a C# source file called garage.cs. It contains classes
corresponding either to the complexType of a top-level element in the input
schema or a type referenced by a top-level element. Let’s run the schema from
listing C.2 in the previous section through the XSD tool and examine the
generated code file.
Each top-level element in the garage.xsd schema file results in a class in
the source code file. The fields of the class are determined by an element’s
complexType. Each xs:element or xs:attribute in the complexType definition
results in a public field in the generated class. Their types are mapped to
according to the standard mapping we’ve seen in Table 5.x and 5.y. If a
top-level element contains an element of another complexType the tool will also
produce a class for the child element’s type.
3 Listing C.3
Classes generated by the XSD tool from the schema in listing C.2
(annotation) <#1 Elements with maxOccurs>1 result in an array type.
>
(annotation) <#2 xs:attribute nodes in the schema result in a field with
an XmlAttributeAttribute. >
(annotation) <#3 The XmlElementAttribute indicates that all items in the
array are direct descendants of the NewDataSet node. >
NOTE: The tool will not produce classes for top-level elements of
simpleTypes. Also, if multiple top-level elements refer to the same complexType
their type, by referencing a named type instead of declaring the type inline,
the tool will only generate a class the first time the complexType is referenced.