我创建了一个架构定义,其开头如下......
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:my.namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:it="urn:mynamespace">
<xs:annotation>
<xs:appinfo>My annotation</xs:appinfo>
</xs:annotation>
Run Code Online (Sandbox Code Playgroud)
然后我加载架构并使用以下命令进行编译:
System.Xml.Schema.XmlSchemaSet set = new System.Xml.Schema.XmlSchemaSet();
set.Add(schema);
set.Compile();
Run Code Online (Sandbox Code Playgroud)
但我无法取回我的注释,我错过了什么?
添加: 感谢Morawski的回复,我最终得到的代码是:
string appInfoValue = string.Empty;
var annotation = schema.Items.OfType<XmlSchemaAnnotation>().FirstOrDefault();
if (null != annotation)
{
var appInfo = annotation.Items.OfType<XmlSchemaAppInfo>().FirstOrDefault();
if (null != appInfo)
{
appInfoValue = appInfo.Markup[0].InnerText;
}
}
Run Code Online (Sandbox Code Playgroud)
嗯,我真的认为应该更简单:)
应该注意的是,该元素的每组允许的子元素在 XmlSchema 类中都有一个相应的属性,除了 elements 之外。这导致一些人认为无法从 XmlSchema 类获取注释,但事实并非如此。可以从 XmlSchema 类的 Items 属性中检索注释。以下代码示例演示如何打印 books.xsd 架构中注释的内容。
System.Xml.Schema 命名空间的秘密(MSDN 上)
(注:日期为 2004 年;未经测试,未经确认)
| 归档时间: |
|
| 查看次数: |
2637 次 |
| 最近记录: |