开发者

Find an annotation for an xml element in its associated schema-defined type

开发者 https://www.devze.com 2023-02-18 22:21 出处:网络
I have an xml type, USAddress, defined in a schema: <xsd:element name=\"MyUSAddress\" type=\"USAddress\"/>

I have an xml type, USAddress, defined in a schema:

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

And an XML element in a data instance document:

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

This schema and instance data are not known at compile time, so all analysis is done dynamically. The simplicity of this schema is only for example purposes. Real schemas will be more complex.

Assuming I have the schema loaded into an System.Xml.Schema.XmlSchema, as I visit each node in my instance xml document, how can I get the associated schema element and read its appinfo annotation?


Use the XMLReader.SchemaInfo property while you're at the node. Then look in SchemaType.Annotation.Items.

Note, your element/annotation nesting is not quite right, but I assume that's only as an unintentional side-effect of the simplification.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号