Chris G
Chris G

Reputation: 57

Reference C# type within XML schema?

Is it possible to reference types within a C# project in an XML Schema file? For validation and intelli-sense I mean...

Lets say I have the following:

namespace Example
{
  class myType
  { int test = 0; }
}

How would I reference the Example namespace within the schema file so that I can use myType as an element type?

Upvotes: 0

Views: 152

Answers (1)

Dan Field
Dan Field

Reputation: 21661

Not directly.

There's sort of two ways you can do this though.

The first is to mark your class as Serializable and use the XmlSerializer to serialize it to XML. In conjunction with this, you can also use xsd.exe to create a schema for your class, and that could be used as a type in your (other) schema. XmlSerializer could be used to serialize and deserialize to/from that type.

On a somewhat related note, if you have a method that you want to capture in an in XML (as opposed to data types or structures), you can extend some XSLT with C# (or VB.NET, or JavaScript) code. MSDN has more documentation on how to do that, the basic idea being to use XsltArgumentList and AddExtensionObject to add that method. It's also possible to do so directly in the XSLT by using msxsl:script (see here for more information on that).

Upvotes: 1

Related Questions