XMLSchema - Defining XmlSchemaElements with sensible types

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)
LVL 13
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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.
Steven_WAuthor Commented:
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.

PAQed with points refunded (400)

Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.