1

I have an XSD file including some patterns and need to create a bunch of XML documents for testing matching this schema. I know there are some tools and frameworks out there like xsd.exe, JAXB, etc. which create classes out of the XSD.

Basically I need to do the following steps:

  • Load XSD
  • Parse XSD
  • Create matching XML document

Example:

<xs:simpleType name="typeName">
  <xs:restriction base="xs:string">
    <xs:pattern value="[A-ZÄÖÜß][A-ZÄÖÜß'/\-.+ ]*"/>
    <xs:pattern value="+[ ]*"/>
  </xs:restriction>
</xs:simpleType>

What would be a standard way doing this? The point is that all this shall happen at runtime, i.e. not creating classes via xsd.exe or else. Preferrably in C# or Java but any language is welcome.

Robert Strauch
  • 12,055
  • 24
  • 120
  • 192

3 Answers3

2

Microsoft has something called the XmlSampleGenerator. Check this out:

http://msdn.microsoft.com/en-us/library/aa302296.aspx

here are it's limitations:

  • The W3C XML Schema Identity Constraints (xs:key, xs:keyref, xs:unique) are not supported while generating an instance document.
  • If xs:pattern facets exist on simple types, values generated may not conform to the pattern.
  • Enumerations of the xs:QName type may not work as expected since this requires the prefixes in the schema to be preserved.
  • xs:ENTITY, xs:ENTITIES, and xs:NOTATION types are not supported.
  • xs:base64Binary content is generated only if enumerations exist in the schema for that type.
mindandmedia
  • 6,800
  • 1
  • 24
  • 33
0

OxygenXml and XmlSpy can generate sample XML files based on XSD templates. Both provide 1 month free trials AFAIK. OxygenXml is cheaper if you want to purchase. If memory serves me correctly neither will populate random strings or suchlike - you'd need to do that yourself. They will however give you a correctly formatted document that matches the schema.

Edit: if you want to generate xml programmatically you could try somethink like this? Might get you out of a hole? I have not tried this myself, just did some googling.

Generate a sample xml document from xsd schema in net

Community
  • 1
  • 1
Nick Ryan
  • 2,662
  • 1
  • 17
  • 24
0

If the XSD is fairly simple (no complex types aside from the 1 that defines the structure of the file) and without a deep hierarchy, you can try to roll your own. I did it once for a personal project (nothing I ever planned to distribute), and it wasn't as bad as I thought. I would also that say the implementation is XSD specific, so if you are planning on generating XML's from many different XSD's or working with unknown XSD's, this is probably not the answer for you.

I'll have to see if I can find the code, but the steps were pretty simple:

  1. Load the XSD as an XDocument
  2. Create a new XDocument for your output XML
  3. Query the XSD with LINQ
  4. Step through each element of the query result and get the name, and add it to the output XDocument

Obviously this will only generate the structure of the XML, it will be up to you to add the values that you want, but once that is done, you can go back and and validate the XML data against the XSD using a traditional method to capture any data errors.

Loading the XSD and parsing it with LINQ is covered in my answer to this question There are also some links at the bottom of the answer for links to other questions/blogs for parsing a more complex XSD if necessary.

Community
  • 1
  • 1
psubsee2003
  • 8,563
  • 8
  • 61
  • 79