如何用超链接替换名称

How to replace names with hyperlinks

我写信是想寻求一些关于我的 c# 替换功能的帮助。下面的 tags() 函数,用故事中的超链接替换标签的名称。当我调用 story() 方法时,我得到以下输出:

The list consists of: ECP 2013-5A C, two slices of HLA 2014-3A D, KEUKA 2013 D, KEUKA 2013-1X D, MVW 2013-1A D, WOODS 2013-10A D, WOODS 2014-11A D and WOODS 2014-11X D. Only one in the past three months – WOODS 2013-10A D at 00 on 00 February.

但是,当我调用 tag() 方法时,我得到以下不正确的输出:

The list consists of: HLA 2014-3A D, KEUKA 2013-1X D, WOODS 2013-10A D, WOODS 2014-11X D. Only one in the past three months –

tags() 函数的输出显示缺失数据(即标签名称)和标签名称未正确超链接(即 www.testdomain.com/data.aspx?searchName=HLA 2014 -3A D>HLA 2014-3A D,

public string tags()
{

    string html = Story();

    DataTable tags = LoadAllTags();

    if (tags.Rows.Count > 0)
    {
        for (int i = 0; i < tags.Rows.Count; i++)
        {

            html = html.Replace(tags.Rows[i][0].ToString(), 
              "<a href=\"http://www.testdomain.com/data.aspx?SearchName=" + tags.Rows[i][0].ToString() + ">" + tags.Rows[i][0].ToString() + "</a>"
            );
        }
    }
    return html;
}

关于我可能哪里出错的任何进一步建议都会非常有帮助。 谢谢

如果我正确理解你的代码,你似乎没有阅读行中的所有相关标签。

尝试以下操作:

public string tags()
{
    string url = "http://www.testdomain.com/data.aspx";
    string html = Story();
    DataTable tags = LoadAllTags();

    if (tags.Rows.Count > 0)
    {
        foreach(var row in tags.Rows)
        {
            foreach(var column in tags.Columns)
            {
                var tag = column.ToString();
                var path = string.Format("{0}?SearchName={1}", url, HttpUtility.UrlEncode(tag);
                var link = string.Format("<a href=\"{0}\">{1}</a>", path, tag);
                html = html.Replace(tag, link);
            }
        }
    }
    return html;
}