在其关联的模式定义类型中查找xml元素的注释

时间:2011-03-19 15:53:40

标签: c# .net xml xsd

我有一个xml类型,USAddress,在模式中定义:

<xsd:element name="MyUSAddress" type="USAddress"/>
<xsd:complexType name="USAddress">
  <xsd:sequence>
    <xsd:element name="name"   type="xsd:string"/>
    <xsd:element name="street" type="xsd:string">
      <xsd:annotation>
        <xsd:appinfo>Special Metadata</xsd:appinfo>
      </xsd:annotation>
    </xsd:element>
    <xsd:element name="city"   type="xsd:string"/>
    <xsd:element name="state"  type="xsd:string">
      <xsd:annotation>
        <xsd:appinfo>Special Metadata</xsd:appinfo>
      </xsd:annotation>
    </xsd:element>
    <xsd:element name="zip"    type="xsd:decimal"/>
  </xsd:sequence>
</xsd:complexType>

数据实例文档中的XML元素:

<MyUSAddress>
   <name>Robert Smith</name>
   <street>8 Oak Avenue</street>
   <city>Old Town</city>
   <state>PA</state>
   <zip>95819</zip>
</MyUSAddress>

此模式和实例数据在编译时是未知的,因此所有分析都是动态完成的。此模式的简单性仅用于示例目的。真正的模式会更复杂。

假设我已将模式加载到System.Xml.Schema.XmlSchema中,当我访问实例xml文档中的每个节点时, 如何获取关联的模式元素并读取其appinfo注释?

1 个答案:

答案 0 :(得分:3)

当您在节点时使用XMLReader.SchemaInfo property。然后查看SchemaType.Annotation.Items。

注意,你的元素/注释嵌套不太正确,但我认为这只是简化的无意的副作用。

相关问题