all groups > dotnet xml > march 2004 >
You're in the

dotnet xml

group:

Building a schema from a class/object at runtime


Building a schema from a class/object at runtime HJ Rodriguez
3/25/2004 5:20:58 PM
dotnet xml: Hello all,

I have the following serializable class.
----------------------------------------------------------------------------
--
Public Class Test

_testID as int32
_testName as String

Public Sub New()
MyBase.New()
End Sub

<XmlAttributeAttribute()> Public Property TestID() As int32
Get
Return _testID
End Get
Set(ByVal Value As int32)
_testID= value
End Set
End Property

<XmlAttributeAttribute()> Public Property TestName() As String
Get
Return _testName
End Get
Set(ByVal Value As String)
_testName= value
End Set
End Property
End Class
----------------------------------------------------------------------------
-

I know I can use XSD.exe to build an XSD file from this class at design
time. But is their any way to build an XSD schema at runtime (in memroy)
based on my class.

I know I can use reflection to query the members of my objects and build my
schema, but I was hoping that there might me something in the .net framework
which can acomplish this more efficiently.

Thank you in advance for your help.

Reguards,
HJ Rodriguez

Re: Building a schema from a class/object at runtime Dino Chiesa [Microsoft]
3/31/2004 4:38:17 PM
It is possible using some undocumented pieces of the System.Xml namespace.
This is a C# code snippet; translation to VB.NET is left to you.

Type cusType = typeof(MyType);

XmlReflectionImporter imp = new XmlReflectionImporter();
XmlSchemas schemas = new XmlSchemas();
XmlSchemaExporter exp = new XmlSchemaExporter(schemas);
XmlTypeMapping xmlTypeMapping = imp.ImportTypeMapping(cusType);
exp.ExportTypeMapping(xmlTypeMapping);

TextWriter tw = new StreamWriter("MyType.xsd");
schemas[0].Write(tw);
tw.Close();

source:
http://www.xmlwebservices.cc/index_FAQ.htm

-Dino



[quoted text, click to view]

AddThis Social Bookmark Button