c# generate xsd from xml programmatically code example

Example: c# generate xml from xsd at runtime

public XmlDocument GenerateEmptyPdfTemplate(GenerateEmptyPdfTemplateDto generateEmptyPdfTemplateDto)
{
  XmlDocument doc = new XmlDocument();

  XmlSchemaSet schemaSet = new XmlSchemaSet();
  // generateEmptyPdfTemplateDto.XsdDocument <--string xsd
  schemaSet.Add(null, XmlReader.Create(new StringReader(generateEmptyPdfTemplateDto.XsdDocument)));

  schemaSet.Compile();


  XmlSampleGenerator generator = new XmlSampleGenerator(schemaSet, new XmlQualifiedName("foo"));
  generator.MaxThreshold=1;
  doc = new XmlDocument();
  using (XmlWriter writer = doc.CreateNavigator().AppendChild())
  {
    generator.WriteXml(writer);
  }

  return doc;
}