Reegenerator comes with a built in Xsd Generator. It generates a Class based on an xml schema (.xsd) file. Using the generated SchemaFactory we can then create an instance of the class from an Xml file using the same schema.
It works similar to xsd.exe when given an .xsd file but it has a few more options.
DoNotGenerateDoCreate If true, the classes generated by XSD.exe as public will be generated as internal instead.
GenerateArraysAsLists If true, the array properties which are generated by XSD.exe as arrays will be generated as lists instead.
GenerateFactoryClass If true, the classes generated by XSD.exe as public will be generated as internal instead.
GenerateInternalClasses If true, the classes generated by XSD.exe as public will be generated as internal instead.
RemoveClassesForIncludedSchemas If true, the classes defined from included schemas which are emitted by XSD.exe as arrays will be not
RemoveDebuggerAttribute If true, the classes defined from included schemas which are emitted by XSD.exe as arrays will be not
XMLData.xml is the data we are going to process.
To generate the schema we can use the aforementioned xsd.exe. Or use one of the many online xsd generator .
The resulting schema is ContactInfo.xsd
This step is optional, but if we set this schema for XmlData.xml we will get warnings about invalid xml data that doesn't adhere to the schema.
Then we attach the xsd renderer to the schema. You can choose
Attach Xsd Renderer. But in this case we choose
Attach Renderers to set the renderer properties.
Select Xsd Renderer from the BuiltIn tab.
The set the template property GenerateArraysAsLists and GenerateFactoryClass to True
Once the Xsd Renderer is attached it generates a .designer.cs containing the Class
As expected, the class is named Person and the children elements (Name, Age, Address, Phones) are its properties. The Address element is generated as a separate Class. And Phones are generated as a List.
Once this is all setup, we can easily consume an xml input and convert it to a Class instance by using the generated ContactInfoFactory
In this sample the XmlData.xml is read and converted into a Person class instance. The bindingSource.DataSource is set to this instance. And finally the data is shown in the controls, bound to the bindingSource, on the right hand side. You can edit the xml and click parse to see the parsed Person properties.