title: Create XML from XSD with sample data in c# programmatically
body: <p>I have an XSD schema in a local file on disk. This XSD can get updated any time, or changed with another one. So it is dynamic, and I can't grab it or keep somewhere.</p>
  
  <p><strong>My goal</strong> is to be able to get a list of all possible XML elements, with their respectful ancestor paths, printed out (in console) Example:
  <code>
  Desired Console output:
  Html
  Head - Html
  Style - Head - Html
  Body - Html
  div - Body - Html
  p - div - Body - Html
  span - div - Body - Html
  strong - div - Body - Html</code></p>
  
  <p>Note: I am not using Actual Html DOM, I am using it to explain the hierarchy.</p>
  
  <p><strong>Simple solution</strong> (I hoped would work...) Parse XSD and filter all instances of elements that have <strong>name="[anyHtmlTag]"</strong>, this way I am able to get a list of all possible elements, but not paths to their Parents. My XSD uses  that refers to a type that is located somewhere deeper in XSD, here is a brief example:
  <a href="https://i.stack.imgur.com/4IDh7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4IDh7.png" alt="My XSD Example"></a></p>
  
  <p><strong>Problem</strong> is that because I am in <strong>Sequence</strong>, "span", "p" or "strong" property cannot see it's parent "div" in this case.</p>
  
  <p><code>
  Actual result that is not desired
  Html
  Head - Html
  Style - Head - Html
  Body - Html
  div - Body - Html
  p
  span 
  strong 
  </code></p>
  
  <p>The reason why It can't see it, is because I am parsing (in C#) XSD schema as if it would be XML document.</p>
  
  <p>In this way I always expect that as hierarchy goes deeper, it will always see it's ancestor.</p>
  
  <p>So my code here works pretty good, until it hits the <strong>Sequence reference</strong> in XSD, and it gets the element, but fails to get it's ancestors.</p>
  
  <p><strong>C# code example</strong></p>
  
  <pre><code>public List&lt;string&gt; GetAllXmlPaths(out List&lt;ValidationResult&gt; errors)
      {
          errors = new List&lt;ValidationResult&gt;();
          List&lt;string&gt; foundPaths = new List&lt;string&gt;();
          string path = @"C:\temp\XSD.xsd"; // this is dynamic
          XmlTextReader reader = new XmlTextReader(path);
          XmlSchema myschema = XmlSchema.Read(reader, ValidationCallback);
          StringWriter sw = new StringWriter();
          myschema.Write(sw);
          string XSD = sw.ToString();
          var xs = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
          var doc = XDocument.Parse(XSD);
          foreach (var element in doc.Descendants(xs + "element"))
          {
              // check ancestors
              string pathToTheTop = "";                
              if (element.Ancestors(xs + "element").Any())
              {
                  foreach (var p in element.Ancestors(xs + "element"))
                  {
                      pathToTheTop = pathToTheTop+ "\\" + p.Attribute("name").Value;
                  }
              }
              foundPaths.Add(element.Attribute("name").Value + ":" + pathToTheTop);
              Console.WriteLine(element.Attribute("name").Value + ":" + pathToTheTop);
          }
          return foundPaths;
      }
  </code></pre>
  
  <p><strong>Why I need to create XML?</strong> In my opinion so far, solution would be to programmatically create a XML with sample data (does not matter what data) but it has to generate XML with all possible paths that XSD provides. In this way all ancestors would be present, and I could parse all hierarchy with ease.</p>
  
  <p>VS has a build-in feature, that allows you to right-click and generate a sample XML out of XSD via <strong>XMLSchemaExplorer</strong>. more info here: <a href="https://stackoverflow.com/questions/14394310/generate-a-test-xml-from-xml-schema-programmatically">Generate a test XML from XML Schema programmatically</a> So I need exact same functionality, but do that programmatically (while I am still in my run-time). </p>