By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

Get Xml schema from user defined type at runtime?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.