Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

xml

xsd

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?

like image 372
Jason Kleban Avatar asked Dec 02 '25 18:12

Jason Kleban


1 Answers

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.

like image 168
xan Avatar answered Dec 05 '25 08:12

xan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!