当代码使用html标记时,Visual Studio无法识别我的代码段

时间:2013-06-09 09:53:13

标签: vb.net visual-studio code-snippets

当我在我的代码中的任何地方使用“< a><![CDATA []]>< / a> .Value ”然后我尝试导出以后导入为Snippet,然后VisualStudio无法识别该片段,因为标签存在冲突。

例如,在这个变量中,我使用标签:

Dim RegEx As New System.Text.RegularExpressions.Regex( _
<a><![CDATA[(http://|https://|www).*\.html?]]></a>.Value)

...所以我可以把它放在一个.snippet文件中,但是我不能使用它,因为标签与片段文件的其他标签冲突,所以VS无法识别片段文件。

enter image description here

enter image description here

enter image description here

我如何解决这个问题?

PS:我不能使用双引号“”代替“&lt; a&gt;&lt;![CDATA []]&gt;&lt; / a&gt; ; .value的

这是一个示例代码段:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>
         Regex match htm html
      </Title>
      <Author>Elektro H@cker</Author>
      <Description>
         Expresión regular para encontrar urls.htm
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>
      </Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>aaaaaaaaa</ID>
          <ToolTip>sfsdf</ToolTip>
          <Default>
          </Default>
          <Function>sdfsdf</Function>
        </Literal>
      </Declarations>
      <Code Language="vb"><![CDATA[

#Region " RegEx Match htm-html "

    ' [ RegEx Match htm-html Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    ' Dim str As String = <a><![CDATA[href="http://www.mp3crank.com/the-rolling-stones/deluxe-edition.htm"]]></a>.Value
    ' MsgBox(RegEx_Match_htm_html(str)) ' Result: http://www.mp3crank.com/the-rolling-stones/deluxe-edition.htm

    Private Function RegEx_Match_htm_html(ByVal str As String, Optional ByVal Group As Int32 = 0) As String

        ' Match criteria:
        '
        ' http://text.htm
        ' http://text.html
        ' https://text.htm
        ' https://text.html
        ' www.text.htm
        ' www.text.html

        Dim RegEx As New System.Text.RegularExpressions.Regex( _
        <a><![CDATA[(http://|https://|www).*\.html?]]></a>.Value)

        Return RegEx.Match(Str).Groups(Group).ToString

    End Function

#End Region

]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

1 个答案:

答案 0 :(得分:3)

您可以将CDATA结束标记设为文字。 试试这个:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>
         Regex match htm html
      </Title>
      <Author>Elektro H@cker</Author>
      <Description>
         Expresión regular para encontrar urls.htm
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>
      </Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>aaaaaaaaa</ID>
          <ToolTip>sfsdf</ToolTip>
          <Default>
          </Default>
          <Function>sdfsdf</Function>
        </Literal>
        <Literal Editable="false">
          <ID>cdataend</ID>
          <ToolTip>Part of the CDATA end tag.</ToolTip>
          <Default>&gt;</Default>
        </Literal>
      </Declarations>
      <Code Language="vb"><![CDATA[

#Region " RegEx Match htm-html "

    ' [ RegEx Match htm-html Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    ' Dim str As String = <a><![CDATA[href="http://www.mp3crank.com/the-rolling-stones/deluxe-edition.htm"]]$cdataend$</a>.Value
    ' MsgBox(RegEx_Match_htm_html(str)) ' Result: http://www.mp3crank.com/the-rolling-stones/deluxe-edition.htm

    Private Function RegEx_Match_htm_html(ByVal str As String, Optional ByVal Group As Int32 = 0) As String

        ' Match criteria:
        '
        ' http://text.htm
        ' http://text.html
        ' https://text.htm
        ' https://text.html
        ' www.text.htm
        ' www.text.html

        Dim RegEx As New System.Text.RegularExpressions.Regex( _
        <a><![CDATA[(http://|https://|www).*\.html?]]$cdataend$</a>.Value)

        Return RegEx.Match(Str).Groups(Group).ToString

    End Function

#End Region

]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

现在应该可以工作。