字符串替换与列表字典数据不起作用

时间:2014-12-12 05:54:15

标签: c# html replace

我试图用来自ListDictionary的值替换html中的标记键。但它没有按我的预期工作。任何人都可以给我一个解决方案。

我的替代方法

    public static string GetDataAddedTemplate(string htmlTemplate, ListDictionary replacements)
    {
        foreach (DictionaryEntry item in replacements)
        {
            htmlTemplate.Replace(item.Key.ToString().ToLower(), item.Value.ToString());
        }
        return htmlTemplate;
    }

我的html模板如下:

    <html>
<body>
        <table align="left" border="0">
        <tr>
            <td>
                <table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 200px;">
                    <thead>
                        <tr style="text-align:center">
                            <th>Bill To</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr style="text-align:center">
                            <td>
                                <<username>>
                                    <br />
                                    <br />
                                    <br />
                                    <br />
                                    <br />
                            </td>
                        </tr>

                    </tbody>
                </table>
            </td>
            <td>
                <table align="left" border="1" cellpadding="1" cellspacing="1">
                    <thead>
                        <tr style="text-align:center">
                            <th colspan="2">Payment information</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                Date
                            </td>
                            <td><<createddate>></td>
                        </tr>
                        <tr>
                            <td>Card holder</td>
                            <td><<cardname>></td>
                        </tr>
                        <tr>
                            <td>Card</td>
                            <td><<cardbrand>> ############<<cardnumber>></td>
                        </tr>
                        <tr>
                            <td>Amount paid</td>
                            <td>$<<amountpaid>></td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>
        Congratulations! You've successfully purchased the <<shares>> plan.
    </p>
    <table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
        <thead>
            <tr style="text-align:center">
                <th>Qty</th>
                <th>Plan</th>
                <th>Description</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr style="text-align:center">
                <td>1</td>
                <td><<shares>> Plan</td>
                <td></td>
                <td>$<<amountpaid>></td>
            </tr>
        </tbody>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>
        <strong>Thank You</strong>!<br />
        Your business is appreciated
    </p>
    <p>&nbsp;</p>
    <p>Team</p>
</body>
</html>

And my ListDictionary is as follows

    ListDictionary replacements = new ListDictionary
                                       {
                                           { "<<UserName>>", "DisplayName" }, 
                                           { "<<CardName>>", "Name" }, 
                                           { "<<CardNumber>>", "Last4" }, 
                                           { "<<CardBrand>>", "Brand" }, 
                                           { "<<AmountPaid>>", "Amount" }, 
                                           { "<<CreatedDate>>", "chargeDetails" }, 
                                           { "<<Shares>>", "shares" }
                                       };

2 个答案:

答案 0 :(得分:0)

试试这个:

        foreach (DictionaryEntry item in replacements)
    {
        htmlTemplate = htmlTemplate.Replace(item.Key.ToString().ToLower(), item.Value.ToString());
    }

答案 1 :(得分:0)

string是不可变的。因此,调用String.Replace会返回一个新字符串,并且不会更改现有字符串。

您需要使用返回的字符串:

public static string GetDataAddedTemplate(string htmlTemplate, ListDictionary replacements)
{
    foreach (DictionaryEntry item in replacements)
    {
        // Use the string returned by Replace method
        htmlTemplate = htmlTemplate.Replace(item.Key.ToString().ToLower(), item.Value.ToString());
    }
    return htmlTemplate;
}