BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
Schemas, xsd, xdr
XSD Schemas
Convert XDR to XSD
Learn XSD Schemas .NET Tutorial
Conversion of genealogical files to XML with a DTD
AccessXML
Sorting by date without using a schema
Validating and enforcing a list of attribute values
Entity name support without DTDs
The Loss of Formatting When Adding a Schema
simple xml notepad
Schemas using DBMS_XMLQuery Package
Tips for Designing a DTD - Part 1 of 4
Tips for Designing a DTD- Part 2 of 4 : Semi-Structured Documents
Tips for Designing a DTD- Part 3 of 4 : Database Oriented Data
Tips for Designing a DTD- Part 4 of 4 : Object Oriented Data
Getting the value of an element with optional subelements
XML Code Generator: Generating Wrapper Classes from an XML Schema
What is WDDX?
XSLT Stylesheet for summarizing the structure of an XML document
Validating XML with Schemas
<< reBlogger
SEO >>

By :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:

<?xml version=”1.0”?>

  <xs:schema id="NewDataSet"
    targetNamespace="urn:GarageSchema.xsd"
    xmlns:mstns="urn:GarageSchema.xsd"

    xmlns="urn:GarageSchema.xsd"

<…>

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

[System.Xml.Serialization.XmlRootAttribute(Namespace="",

  IsNullable=false)]

public class garage {

    public string desc;

    [System.Xml.Serialization.XmlElementAttribute("car")]

    public garageCar[] car;                                | #1

}

public class garageCar {

    [System.Xml.Serialization.XmlAttributeAttribute()]     | #2

    public string make;   

    [System.Xml.Serialization.XmlAttributeAttribute()]

    public string model;

    [System.Xml.Serialization.XmlAttributeAttribute()]

    public string year;

}

[System.Xml.Serialization.XmlRootAttribute("NewDataSet", Namespace="", IsNullable=false)]

public class NewDataSet {   

    [System.Xml.Serialization.XmlElementAttribute("garage")] |#3

    public garage[] Items;

}

(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.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
masks
swimming pool builder
free conference call
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP