How can I merge XML comments back into a .cs file?

281 views Asked by At

Extracting comments out of C# class files is relatively easy (see Extracting doc comments from C# source file), but I've recently come into the opposite problem.

My project has a bunch of classes that are generated from XML schema (via Microsoft's xsd.exe). I'd like to write out XML documentation on these classes, but we have to recreate them every so often. I'd like to be able to write out the comments, extract them into their own .xml file, run xsd.exe to recreate the classes from the schema, and then merge the comments back in.

Is there some way to do this?

1

There are 1 answers

2
fejesjoco On BEST ANSWER

I peeked inside xsd.exe of the v8.1A version of the SDK (it was written in .NET so you can use a tool like JustDecompile to see what it does). The method which drives the XSD->C# generation is called ImportSchemasAsClasses. What it does is read the XSD files into a raw XmlSchemas instance, import the schemas with the XmlSchemaImporter class (into an abstract model called type mapping), then generate code with the XmlCodeExporter class into Code DOM classes. As you can see from the links, these are public, but undocumented API's. But you can easily replicate what xsd.exe does (you only need to call public .NET API's, just copy and paste what xsd.exe does) and tap into the right place to add some comments into the Code DOM, nice and clean, no string manipulations needed.