473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get Xml schema from user defined type at runtime?

To whoever can help,

Is there a way to get the Xml schema for a user defined type at runtime?

I've looked at the XmlSerializer as I had hoped there might be a way to
retrieve it. I've also looked at XmlReader and XmlWriter, but again no luck.

I am trying to generate a schema for an arbitrary collection of objects and
native types at runtime. The basic idea is that I have a collection of
key/value pairs where the value can be of any native or user defined type. I
want to provide the schema for any arbitrary subset of this collection when I
give the client the subset to work with.

Perhaps I'm looking at this the wrong way. If you can turn me around I
would greatly appreciate it.

Thanks,
John Glover
Mar 21 '06 #1
3 2028
Hi John,

This is what xsd.exe do to get the schema from a class. I think you have
two options here:

1. Write your class to a file and invoke xsd.exe to get the output schema.
This will require additional files to be generated.
2. Implement your own schema generator. You can use the reflector to check
what xsd.exe is doing. It's not that complex.

Hope that helps.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mar 22 '06 #2
Thanks for your quick response. I took your second suggestion and, using
Reflector.exe to peek inside xsd.exe, figured out how the schema generation
was being done there. I thought others might be interested in the final
result, so I'm going to post it here. I'd also love any feedback if somebody
can see a better way to do it. For some reason this code feels a little
hacked together for me.

Anyway, here is the code:

<code>
public static XmlSchema GetSchemaForType ( Type type ) {
XmlReflectionImporter importer = new XmlReflectionImporter ( );
XmlSchemas schemas = new XmlSchemas ( );
XmlSchemaExporter exporter = new XmlSchemaExporter ( schemas );

XmlTypeMapping mapping = importer.ImportTypeMapping ( type );
exporter.ExportTypeMapping ( mapping );

// NOTE: If you want to receive validation events, you must pass
in an
// appropriate ValidationEventHandler delegate for the first
parameter
schemas.Compile ( null, false );

return schemas[mapping.Namespace];
}
</code>

Thanks,
John Glover

"Kevin Yu [MSFT]" wrote:
Hi John,

This is what xsd.exe do to get the schema from a class. I think you have
two options here:

1. Write your class to a file and invoke xsd.exe to get the output schema.
This will require additional files to be generated.
2. Implement your own schema generator. You can use the reflector to check
what xsd.exe is doing. It's not that complex.

Hope that helps.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mar 23 '06 #3
Hi John,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mar 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jari Kujansuu | last post by:
I can successfully parse XML document using SAX or DOM and I can also validate XML document against schema. Problem is that my program should deal with user-defined schemas which means that when...
0
by: Sebasti?n Streiger | last post by:
Hi everybody! I´m new to XML and I need to generate an XML document. I should suppport the outpus from an SQL query that may be user defined. So I need to export that data to an XML document and...
3
by: Rohit Sharma | last post by:
Hi all.. ..NET + MSXML platform....VB Need to build a list of all the entities and attributes to allow the user to do search...how can i do that from the schema ?? Cheers Rohit
2
by: Rajesh Jain | last post by:
I Have 2 separate schemas. --------------Schema 1 is defined as below----------- <xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema"...
2
by: PeterW | last post by:
I have an xml file from which I want to generate an xsd schema and at a later stage a cs class. The xml file has a mix of defined namespaces and also an empty namespace. These are defined as...
0
by: Kurt | last post by:
Hi, (sorry if I'm cross posting - not quite sure where to put) I am basically trying to force the user of my web service to only send a valid dataset that is based on a schema I have created. ...
3
by: MR | last post by:
Is there a way to load an XML schema into a dataset from a resource or other object? I am using ReadXmlSchema(filename) and that works fine. However, I have many XSD files that I would rather not...
0
by: rautsmita | last post by:
hello friends , i am using to jdk6 and JAXB2.0, i have geomtry.xsd file i am trying to compile this file using jaxb but i got some error i.e.The particle of the type is not a valid restriction of...
2
by: aj | last post by:
SQL Server 2005 64-bit 9.00.3042 SP2 When I map a database user to a login, I can specify a default schema for that user. After that, any SQL from that user w/o an explicit schema will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.