I have an XML Schema that I’m trying to create C# classes with. At the moment, I’m working with XSD2Code to create the classes. The original schemas have elements that look something like this:
<xs:element name="NumberOfTags" type="xs:positiveInteger" minOccurs="0">
<xs:annotation>
<xs:documentation>ALT\N</xs:documentation>
</xs:annotation>
</xs:element>
XSD2Code produces code that looks something like this:
/// <summary>
/// ALT\N
/// </summary>
public string NumberOfTags { get; set; }
The XML comment refers to a TMATS CODE that maps to a different data representation. I need to capture these codes in some sort of runtime-readable representation so that I can perform this mapping programmatically. My best idea to achieve this so far is to translate these XML comments to C# Attributes, and write additional code to reflect over them.
So here is my question. I would like to do a regex-style replace on the code generated by XSD2Code, using Visual Studio Find and Replace, so that the resulting code looks like this:
[TmatsCode(@"ALT\N")]
public string NumberOfTags { get; set; }
What is the regex that would achieve this?
Using Visual Studio’s Find & Replace, you can use this expression:
And this replacement:
To break it down:
^.*\<summary\>\n– Match on the beginning of a line, any amount of characters uptil followed by a new line.{:b*}///capture any whitespace up til the the /// characters. We’ll later use this captured whitespace so we can indent the attribute correctly.:b*{.*}match any whitespace, then capture the remainder as the content we want.\n.*\<\/summary\>$match on the new line, then till the end of the line of the closing summary element.For the replacement,
\1is the whitespace we captured so the attribute is indented to the same place, and\2is the summary content.