在 XML 模式中引用 C# 类型?

Reference C# type within XML schema?

是否可以在 XML 架构文件中引用 C# 项目中的类型?对于验证和智能感知,我的意思是...

假设我有以下内容:

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

如何在架构文件中引用 Example 命名空间,以便我可以将 myType 用作元素类型?

不直接。

虽然有两种方法可以做到这一点。

首先是将你的class标记为Serializable并使用XmlSerializer将其序列化为XML。与此结合,您还可以使用 xsd.exe 为您的 class 创建模式,并且 that 可以用作您的(其他)模式中的类型. XmlSerializer 可用于序列化和反序列化 to/from 该类型。

在某种程度上相关的注释中,如果您有一个 方法 并希望在 XML 中捕获(而不是数据类型或结构),您可以用 C#(或 VB.NET,或 JavaScript)代码扩展一些 XSLT。 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 了解更多信息)。