Do not pass literals as localized parameters

I have the following warning when performing code analysis in my project (which is a Windows Phone 8.1 application):

CA1303 Do not pass literals as localized parameters. The "Common.TranslateError (String)" method passes a literal string as the "text" parameter of the call to "XDocument.Parse (String)". Instead, take the next row (s) from the resource table.

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 this is not what I had to solve after adding the code, however this warning makes me believe that I am not doing something completely right.

I did some research on this and found the MSDN article CA1303: do not pass literals as localized parameters , however I cannot link to ResourceManager. If I could reference this, I would still not understand why this is a problem when passing a string to XDocument.Parse.

I want to address the warning and not suppress it. Does anyone have any ideas how I can fix this or why does such a warning exist?

If you want to replicate, you will need to configure the Rule Set to use All Microsoft rules :

enter image description here

Then, to start the analysis, select ANALYZE from the Visual Studio menu and select Analyze execution code on ...

+4
1

@RyanRoos, :

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
0

Source: https://habr.com/ru/post/1660387/


All Articles