1

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?

0

2 Answers 2

2

using Visual Studio Find and Replace

Using Visual Studio's Find & Replace, you can use this expression:

^.*\<summary\>\n{:b*}///:b*{.*}\n.*\<\/summary\>$

And this replacement:

\1[TmatsCode(@"\2")]

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, \1 is the whitespace we captured so the attribute is indented to the same place, and \2 is the summary content.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, thanks! One of these days, I'm going to learn regex. It's on my todo list with about a thousand other things to learn. :/
2
(?<=/// \<summary\>\r\n/// )[^\r\n]+(?=\r\n/// \<\/summary\>)

will match

ALT\N

in your example.

EDIT:

VS find and replace? Didn't notice that... the above is .net Regex.

However, continuing in this direction and in answer to your comment... By rewriting the Regex a little to match the entire comment and capturing the inner value in a group, the MatchEvaluator overload of Regex.Replace comes into its own here:

Regex.Replace(
 str,
 @"/// \<summary\>\r\n/// (?<tag>[^\r\n]+)\r\n/// \<\/summary\>",
 m => string.Format(
       "[TmatsCode(@\"{0}\"]",
       m.Groups["tag"].Value));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.