looking for a good XML schema wrapper tool/code generator
We extensively use XML schema in our application, exchanging XML instances between the client and the server.
We create typed dataset wrappers on top of the schema to speed up the development. These wrappers are just used internally and are not exchanged between the system components.
Some features enabled using the typed dataset as a wrapper are missingin from the classes generated by XSD.exe so we dont' use it.
On the other hand dataset aren't so good at managing schemas with imported namespace, any and so on.
Is there any third party tool that is able to create rich and complete wrappers around XSD schemas? We are currently working on .Net 2.0 Beta 2.
Thanks
[687 byte] By [
DavideB] at [2008-1-19]
xsd.exe also has a /c option which generates simple C# classes (rather than typed datasets). Using xsd.exe that comes with .NET 2.0 I was able to take these two schemas:
<xs
chema xmlns:x="uri:bar" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="uri:foo" xmlns:xs="http://www.w3.org/2001/XMLSchema"><br/>
<xs:import namespace="uri:bar" schemaLocation="bar.xsd" /><br/>
<xs:element name="foo"><br/>
<xs:complexType><br/>
<xs
equence><br/>
<xs:element ref="x:bar" /><br/>
</xs
equence><br/>
</xs:complexType><br/>
</xs:element><br/>
</xs
chema>
and
<?xml version="1.0" encoding="utf-8"?>
<xs
chema xmlns:tns="uri:bar" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="uri:bar" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bar" type="tns
ow"/>
<xs
impleType name="dow">
<xs:restriction base="xs
tring">
<xs:enumeration value="Sunday"/>
<xs:enumeration value="Monday"/>
<xs:enumeration value="Tuesday"/>
<xs:enumeration value="Wednesday"/>
<xs:enumeration value="Thursday"/>
<xs:enumeration value="Friday"/>
<xs:enumeration value="Saturday"/>
</xs:restriction>
</xs
impleType>
</xs
chema>
And run xsd.exe with the following command line:
xsd.exe foo.xsd bar.xsd /c
And this produced a file called foo_bar.cs which contained the following:
using System.Xml.Serialization;
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="uri:foo", IsNullable=false)]
public partial class foo {
private dow barField;
[System.Xml.Serialization.XmlElementAttribute(Namespace="uri:bar")]
public dow bar {
get {
return this.barField;
}
set {
this.barField = value;
}
}
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="uri:bar")]
[System.Xml.Serialization.XmlRootAttribute("bar", Namespace="uri:bar", IsNullable=false)]
public enum dow {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
}
When I serialized an instance of the foo class I got the following valid XML:
<foo xmlns="uri:foo">
<bar xmlns="uri:bar">Sunday</bar>
</foo>
Thank you for the response.
As I wrote in my post, we don't use XSD.exe to create classes as few features are missing from it while are available with typed datasets.
Just as an example, the Is<nullable element name>Null and Set<nullable element name>Null managed by the procedures are really productive.
Yes, XSD.exe is able to better manage schemas but what we need is something between typed datasets and XSD.exe.
Do you know any tool that is able to create something like this?
There is a lot of functionality in DataSet that is very handy for manipulating data, and DataSet is also efficient in how it stores large sets of typed values. It provides sql-lite semantics and binds nicely to DataGrids and so forth. So yes, there are lots of reasons to want to use DataSet. But as you have noticed, the price you pay for all that is limited support for the "richness" of XSD.
On the other hand programming your own objects you can support more of XSD, but then you have to write more code to get up to par with the features you like in DataSet. Your example of IsNullable is a good example of this. If I were to implement that myself I would use the new C# 2005 support for nullable that is built into the language as follows:
class foo {
int? bar;
public int? Bar { get { return this.bar; } set { this.bar = value; }
}
Then you can write the following code:
foo f = new foo();
if (f.Bar.HasValue) {
Console.WriteLine(f.Bar.ToString());
}
So this allows you to then have any type be nullable. As for Select like functionality, you could wrap your objects in an XPathObjectNavigator (available for download from MSDN) and party on your object graph using XPath expressions.