Click here to Skip to main content
12,952,215 members (61,506 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I've been trying to create an XML document and corresponding XSD schema in C#.
I can create the XML fine and get the XSD, but the XSD isn't 100% correct.
The attribute type for my elements is always xs:string in the XSD, which is not correct.
Some of them need to be integer and others string.
I'm new to this so the answer might be pretty obvious but I'm just not sure how I go about doing it. I think I need to use XmlSerializer for it from what I have read but I'm not sure where to start with it.

Below of a a simple example of what I am trying to do:

class Person
    public int ID { get; set; }
    public String Name { get; set; }
    public int Age { get; set; }
    public XElement GetXML()
        var person = new XElement("Person", new XAttribute("ID", ID));
        person.Add(new XElement("Name", Name));
        person.Add(new XElement("Age", Age));
        return person;
static void Main(string[] args)
    var set = new DataSet("Test");
    var root = new XElement("root");
    var p1 = new Person() {ID = 1, Name = "P1", Age = 32};
    var p2 = new Person() {ID = 2, Name = "P2", Age = 40};
    var xml = set.GetXml();
    var schema = set.GetXmlSchema();

This code produces the following XSD and XML
<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="root" xmlns="" xmlns:xs="" xmlns:
  <xs:element name="root" msdata:IsDataSet="true" msdata:Locale="en-US">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Person">
              <xs:element name="Name" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
              <xs:element name="Age" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
            <xs:attribute name="ID" type="xs:string" />
  <Person ID="1">
  <Person ID="2">

As you can see, in the XSD the Age element is given a type of string. I assume this is by default, but I cannot see how I am supposed to set this to a different type, such as integer.

Any help would be appreciated.
Posted 11-Jan-13 0:32am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It may or may not give you the exact type of dataset Columns, But at least omit the the string type.
Make this small change in your code and rerun.

For more information, Read about XMLReadMode Enum
Brian Dunne 11-Jan-13 8:32am
Hi Shanu2rick,
Thanks for the reply but I had already messed around with that enum.

Unfortunately as you suggest it doesn't provide the exact type, which for me is sort of important.
For example, Age is given a type of unsignedByte, which while appropriate for the particular value, it means that as the value of age increases to a larger number (or the value of ID, which would be more appropriate), the schema definition will change, which is not useful to me.
I need to be able to specify a type (as in integer for Age, regardless of data size).
Shanu2rick 11-Jan-13 23:49pm
I understand your problem.
The problem with your code is that you are creating a xml schema using C#, then passing it to a dataset, which is not that helpful.
However, you can still change the datatypes of the desired dataset columns, but it won't make any difference in the xml schema type.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100