XMLSchema - Defining XmlSchemaElements with sensible types

Posted on 2006-04-11
Medium Priority
Last Modified: 2008-02-01
I am trying to create a simple XMLSchema (XSD that can be written to a file).

Note: .Net Framework v2.0

My problem comes with trying to assign a (very simple) data-type to the element.

For example - I would expect to find the following output

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Test" type="xs:string" />


My code:

      XmlSchema MySchema = new XmlSchema();

      XmlSchemaElement e = new XmlSchemaElement();
      e.Name = "Test";
      e.SchemaType = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String);

      XmlWriterSettings xws = new XmlWriterSettings();
      xws.CloseOutput = true;
      xws.Indent = true;

      XmlWriter xw = XmlTextWriter.Create("Testing.xsd", xws);


The output:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Test">
      <xs:restriction base="xs:anySimpleType" />


So, I then tried looking at other XmlTypeCodes (createing a Schma with one XmlSchemaElement for every possible value in the XmlTypeCode Enum)

      foreach (string s in Enum.GetNames(typeof(XmlTypeCode)))
        XmlSchemaElement e = new XmlSchemaElement();
        e.Name = s;
        e.SchemaType = XmlSchemaType.GetBuiltInSimpleType((XmlTypeCode)(Enum.Parse(typeof(XmlTypeCode), s)));


This produced a rather strange file (the elenment names didn't match up to the TypeCodes that I expected..


Boolean, Decimal, Float, Decimal, all got mapped to "xs:anySimpleType"
Notation is mapped to "xs:string"
Token is mapped to "xs:normalizedString"
Language is mapped to "xs:token"

Can somebody help explain

What is wrong with my code (or is this a problem with the System.Xml.Schema namespace ?
Is there an easy way to convert the basic inbuilt CLR-types (byte, int, long, short, string, boolean) to a SchemaType (other than writing a long switch() statement)
Question by:Steven_W
  • 2
LVL 12

Expert Comment

ID: 16439038
The MSDN page for the XmlSchemeElement shows it being set like so:

e.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")

Rather odd, as your example seems more proper.
Hope this helps.
LVL 12

Expert Comment

ID: 16439096
Ah ok. the SchemaType property is only for use when the element is of a 'simpleType' or a 'complexType'

In the case where you want <xs:element name="a" type="sometype"/>
they define the property (type="sometype") as the 'SchemaTypeName', an XmlQualifiedName.
LVL 13

Author Comment

ID: 16439108
From my post to http://msdn.microsoft.com/forums/, I was also given another alternative


I half expect there to be aquite a few breaking-changes (or major enhancements) to the System.Xml namespace in the next version of the Framework . It still seems overly complex at the moment, (but with a variety of expected-features missing or hard-to-find.


Accepted Solution

CetusMOD earned 0 total points
ID: 16958105
PAQed with points refunded (400)

Community Support Moderator

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question