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.
精彩评论