Search code examples
c#.netstringstring-formattingresourcedictionary

string.Format exception Input string was not in a correct format


I received the following error from a string.Format(...) operation:

System.FormatException - Input string was not in a correct format.

I have a resource dictionary which contains an entry with a basic html page.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title of the document</title>

    <style>
        * { 
            margin: 0; 
            padding: 0; 
        }

        body { 
            font-family: 'Arial, Verdana', Fallback, sans-serif; 
            font-size: 14px;
        }

        table { 
            background-color: #eeeeee; 
            width: 100%; 
            padding: 10px;
            margin: 5px;
        }

        h1 {
            font-size: 16px;
            font-weight: bold;
            margin: 20px 0 10px 0;
        }
    </style>
</head>

<body>
    text<br />

    <table>
        <tr>
            <td>Text 1</td>
            <td>{0}</td>
        </tr>
        <tr>
            <td>Text 2</td>
            <td>{1}</td>
        </tr>
        <tr>
            <td>Text 3</td>
            <td>{2}</td>
        </tr>
        <tr>
            <td>Text 4</td>
            <td>{3}</td>
        </tr>
        <tr>
            <td>Text 5</td>
            <td>{4}</td>
        </tr>
        <tr>
            <td>Text 6</td>
            <td>{5}</td>
        </tr>
        <tr>
            <td>Text 7</td>
            <td>{6}</td>
        </tr>
    </table>
    <br />

    <h1>Header1</h1>

    <table>
        <tr>
            <td><a href="http://www.google.com">http://www.google.com</a></td>
        </tr>
        <tr>
            <td>Text 8: {7}</td>
        </tr>
    </table>
    <br />
</body>

The string.Format(...) operations looks like this:

var emailHtml = string.Format(
                    WebUtility.HtmlEncode(EmailResource.EmailTemplate),
                    "text1",
                    "text2",
                    "text3",
                    "text4",
                    "text5",
                    "text6",
                    "text7",
                    "text8");

Does anyone know what error I made? Isn't it possible to fill in the placeholders in the resource dictionary like this?


Solution

  • You have a lot of curly braces which are not format specifiers, for example:

        { 
            margin: 0; 
            padding: 0; 
        }
    

    Either you need to duplicate them:

        {{
            margin: 0; 
            padding: 0; 
        }}
    

    Or using different formatting approach (like template engine).