XMLSchema - Defining XmlSchemaElements with sensible types

Posted on 2006-04-11
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="">
  <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="">
  <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
    LVL 12

    Expert Comment

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

    e.SchemaTypeName = New XmlQualifiedName("string", "")

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

    Expert Comment

    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

    From my post to, 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

    PAQed with points refunded (400)

    Community Support Moderator

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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…
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now