Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract enumeration values from xsd schema file in .net

How programmatically extract enumeration constraint values of an element from xsd schema file using .net?

for example I'd like to extract 'Audi', 'Golf' and 'BMW' from the following xsd:

<xs:element name="car">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Audi"/>
      <xs:enumeration value="Golf"/>
      <xs:enumeration value="BMW"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
like image 884
pavliks Avatar asked Mar 01 '23 19:03

pavliks


1 Answers

There is an XmlSchema class, but it looks pretty... "fun" to work with.

Would xml querying be enough?

XmlDocument doc = new XmlDocument();
doc.Load("Foo.xsd");            
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("xx", "http://www.w3.org/2001/XMLSchema");
foreach (XmlElement el in doc.SelectNodes("//xx:element[@name='car'][1]/xx:simpleType/xx:restriction/xx:enumeration", mgr))
{
    Console.WriteLine(el.GetAttribute("value"));
}
like image 195
Marc Gravell Avatar answered Mar 06 '23 21:03

Marc Gravell