generate xml from xsd code example

Example 1: 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;
}

Example 2: generate xsd from xml

XSD is used to validate an XML files.

Tags:

Misc Example