title: Get the complex object tag name from xml schema in C#
body: <p>I would like to ask if there's a way to get the tag names associated to a given "complex type name" (from complex object inside a XSD (XML Schema Definition)) in C# (.NET Framework 4.5).</p>
  
  <p>The result I would like to achieve is: If I search for <code>WhateverTypeName1</code> the value <code>"childoftypeone"</code> should be returned.</p>
  
  <p>Let's say that we have the following XSD excerpt:</p>
  
  <pre><code>&lt;xs:complexType name="ParentType"&gt;
      &lt;xs:choice&gt;
          &lt;xs:element name="childoftypeone" type="WhateverTypeName1"/&gt;
          &lt;xs:element name="childoftypetwo" type="OtherTypeName"/&gt;
      &lt;/xs:choice&gt;
  &lt;/xs:complexType&gt;
  
  &lt;!-- after some declarations --&gt;
  
  &lt;xs:complexType name="WhateverTypeName1"&gt;
      &lt;xs:sequence&gt;
          &lt;!-- other elements from sequence --&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p>From the <code>XmlSchema</code> type, I can get a <code>XmlSchemaComplexObject</code> by searching for <code>WhateverTypeName1</code> this way:</p>
  
  <pre><code>var schema = new XmlSchema(); // load the XSD here.
  
  var arr = new string[] { "WhateverTypeName1" };
  
  var type = schema.Items
      .OfType&lt;XmlSchemaObject&gt;()
  
      // we can search matching the type here, put this way just to be concise
      .Where(w =&gt; w.GetType().Name.ToLower().Contains("complex"))
      .Select(s =&gt; (XmlSchemaComplexType)s)
      .FirstOrDefault(w =&gt; arr.Contains(w.Name));
  </code></pre>
  
  <p>The thing is, from this <code>XmlSchemaComplexType</code> object, I didn't manage to match it with the <code>"childoftypeone"</code> tag declaration on the <code>ParentType</code> (<code>&lt;xs:element name="childoftypeone" type="WhateverTypeName1"/&gt;</code>).</p>
  
  <p>I only managed to get this pairing, if I search for its parent object (<code>ParentType</code>) and walk through its <code>Particle</code> property. However, I guess it's not possible to get where this type is being used (e.g. the <code>ParentType</code>) from its own <code>XmlSchemaComplexType</code></p>
  
  <p>How could I accomplish this?</p>