如何将XML注释合并回.cs文件?

时间:2023-01-24 23:06:55

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.

从C#类文件中提取注释相对容易(请参阅从C#源文件中提取文档注释),但我最近遇到了相反的问题。

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.

我的项目有一堆从XML模式生成的类(通过Microsoft的xsd.exe)。我想在这些类上写出XML文档,但我们必须经常重新创建它们。我希望能够写出注释,将它们提取到自己的.xml文件中,运行xsd.exe以从模式重新创建类,然后将注释合并回来。

Is there some way to do this?

有办法做到这一点吗?

1 个解决方案

#1


3  

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.

我偷看了SDK的v8.1A版本的xsd.exe(它是用.NET编写的,所以你可以使用像JustDecompile这样的工具来查看它的作用)。驱动XSD-> C#生成的方法称为ImportSchemasAsClasses。它的作用是将XSD文件读入原始XmlSchemas实例,使用XmlSchemaImporter类导入模式(进入称为类型映射的抽象模型),然后使用XmlCodeExporter类将代码生成到代码DOM类中。正如您从链接中看到的,这些是公开的,但未记录的API。但是你可以很容易地复制xsd.exe所做的事情(你只需要调用公共.NET API,只需复制并粘贴xsd.exe所做的)并点击正确的位置在Code DOM中添加一些注释,干净整洁,不需要字符串操作。

#1


3  

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.

我偷看了SDK的v8.1A版本的xsd.exe(它是用.NET编写的,所以你可以使用像JustDecompile这样的工具来查看它的作用)。驱动XSD-> C#生成的方法称为ImportSchemasAsClasses。它的作用是将XSD文件读入原始XmlSchemas实例,使用XmlSchemaImporter类导入模式(进入称为类型映射的抽象模型),然后使用XmlCodeExporter类将代码生成到代码DOM类中。正如您从链接中看到的,这些是公开的,但未记录的API。但是你可以很容易地复制xsd.exe所做的事情(你只需要调用公共.NET API,只需复制并粘贴xsd.exe所做的)并点击正确的位置在Code DOM中添加一些注释,干净整洁,不需要字符串操作。