简体   繁体   中英

C# - Parsing XSD schema - get all elements to combobox

I have XSD Schema file and i need to fill my combobox with the elements from the schema file...

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="auto">
      <xs:complexType>
         <xs:sequence>
            <!-- Znacka -->
            <xs:element name="znacka" type="xs:string"/>
            <!-- pocetOsob -->
            <xs:element name="pocetOsob" type="xs:int"/>
            <!-- maxRychlost -->
            <xs:element name="maxRychlost">
               <xs:complexType>
                  <xs:simpleContent>
                     <xs:extension base="xs:decimal">
                        <xs:attribute name="jednotka" type="xs:string"/>
                     </xs:extension>
                  </xs:simpleContent>
               </xs:complexType>
            </xs:element>
            <!-- Motor -->
            <xs:element name="motor">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="vykon">
                        <xs:complexType>
                           <xs:simpleContent>
                              <xs:extension base="xs:decimal">
                                 <xs:attribute name="jednotka" type="xs:string"/>
                              </xs:extension>
                           </xs:simpleContent>
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
                  <xs:attribute name="vyrobni_cislo" type="xs:string"/>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema> 

Anyone had idea how to do it? Through xpath? I have a half working code... I got a message with element auto.

String path = openSchema.FileName;
XmlTextReader xsd_file = new XmlTextReader(path);
XmlSchema schema = new XmlSchema();
schema = XmlSchema.Read(xsd_file, null);

MessageBox.Show(schema.Items.Count.ToString());

foreach (XmlSchemaElement element in schema.Items)
{
    elements.Items.Add(element.Name);
    MessageBox.Show(element.Name);
}

Thank you very much!

string xml = <your xml>;
var xs = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
var doc = XDocument.Parse(xml);
// if you have a file: var doc = XDocument.Load(<path to xml file>)
foreach(var element in doc.Descendants(xs + "element"))
{
    Console.WriteLine(element.Attribute("name").Value);
}
// outputs: 
// auto
// znacka
// pocetOsob
// maxRychlost
// motor
// vykon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM