4

I have the following warning when running a code analysis on my project (which is a Windows Phone 8.1 app):

CA1303 Do not pass literals as localized parameters Method 'Common.TranslateError(String)' passes a literal string as parameter 'text' of a call to 'XDocument.Parse(String)'. Retrieve the following string(s) from a resource table instead.

This is my method:

Public Function TranslateError(ByVal exMessage As String) As XDocument

    Return XDocument.Parse("<Response><Exception><Message><" & XmlConvert.EncodeName(exMessage) & "></Message></Exception></Response>")

End Function

The code works and it's not something I've had to address since adding the code however this warning makes me believe I'm not doing something quite right.

I've done some research into this and found the MSDN acticle CA1303: Do not pass literals as localized parameters however I can't reference to a ResourceManager. If I could reference to that I still wouldn't understand why this is a problem when passing a string to XDocument.Parse.

I want to address the warning and not suppress it. Has anyone got any ideas how I can fix this or why such a warning exists?

Should you want to replicate you will need to configure the Rule Set to use Microsoft All Rules:

enter image description here

Then to run the analysis select ANALYZE from the Visual Studio menu and choose Run Code Analysis on...

Community
  • 1
  • 1
Bugs
  • 4,491
  • 9
  • 32
  • 41
  • I took a look at it for a bit to replicate what you are seeing and got the same results. After making a few adjustments seems to be more of an issue with the named parameter of the Parse function being "text" than anything you have control over. If you can't use a ResourceManager to loads the string values for input, then using a StringBuilder to assemble the values and pass it to the parse function was a quick work around that resolved the warning. – Ryan Roos Nov 10 '16 at 20:43
  • 1
    More info can be found here: https://stackoverflow.com/a/9805039/14344821 – Casper Dijkstra Oct 16 '20 at 15:32

1 Answers1

1

As suggested by @RyanRoos this piece of code resolved the warning:

Public Function TranslateError(ByVal exMessage As String) As XDocument

    Dim sb As New StringBuilder("<Response><Exception><Message><![CDATA[" & XmlConvert.EncodeName(exMessage) & "]]></Message></Exception></Response>")

    Return XDocument.Parse(sb.ToString())

End Function
Bugs
  • 4,491
  • 9
  • 32
  • 41