Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML LINQ
Hi,
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};
 
    root.Add(p1.GetXML());
    root.Add(p2.GetXML());
 
    set.ReadXml(root.CreateReader());
    var xml = set.GetXml();
    var schema = set.GetXmlSchema();
 
    Console.WriteLine(schema);
    Console.WriteLine(xml);
    Console.ReadKey(true);
}
 
This code produces the following XSD and XML
<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="root" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:
msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="root" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Person">
          <xs:complexType>
            <xs:sequence>
              <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:sequence>
            <xs:attribute name="ID" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
 
<root>
  <Person ID="1">
    <Name>P1</Name>
    <Age>32</Age>
  </Person>
  <Person ID="2">
    <Name>P2</Name>
    <Age>40</Age>
  </Person>
</root>
 
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.
Thanks
Posted 11-Jan-13 0:32am

1 solution

Rate this: bad
good
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.
 set.ReadXml(root.CreateReader(),XmlReadMode.InferTypedSchema);
 
For more information, Read about XMLReadMode Enum
  Permalink  
Comments
Brian Dunne at 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).
Thanks,
Brian
Shanu2rick at 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
0 Sergey Alexandrovich Kryukov 403
1 OriginalGriff 354
2 George Jonsson 202
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
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