简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM