[英]How to get the default value from an XmlSchema
我正在使用一个用 XSD 文件初始化的 XmlSchema object。 XSD 文件包含一些验证规则(类型、最小值、最大值等),并且一些元素具有默认值属性。
这是一个简短的例子:
1. XML 文件:
<person>
<first>john</first>
<last>doe</last>
<age>50</age>
</person>
XML 正在加载到 XmlDocument object 中。
2. XSD 文件:
<?xml version="1.0" encoding="iso-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person" >
<xs:complexType>
<xs:all>
<xs:element name ="first" type="xs:string" minOccurs="0"/>
<xs:element name ="last" type="xs:string" minOccurs="0"/>
<xs:element name ="age" type="xs:integer" minOccurs="0" default="30"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
XSD 正在加载到 XmlSchema object。
3.我想在不执行验证和常规XML解析的情况下检索age的默认值,如果我必须执行验证,如果节点验证成功,我该如何检索该值(验证处理程序成功时不会被调用)?
我尝试使用IXmlSchemaInfo.SchemaElement
获取值,但在验证发生后它被设置为 null 。
好的,一些澄清:
默认值是指用户未指定年龄元素时的默认值。 XML 可能是:
<person>
<first>john</first>
<last>doe</last>
<age></age>
</person>
我会尝试更精确:
MSDN 上有一个关于如何遍历XmlSchema
object 的代码示例: Traversing XML Schemas 。
这样您就可以读取给定元素的default
属性的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.