读取指定目标的HTML文件

本文介绍了一种处理模板文件的方法,通过遍历数据行并根据特定规则动态替换模板中的占位符,实现产品信息的展示。具体步骤包括获取目标文件路径、读取文件内容、定位目标节点及进行占位符的动态替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模板文件($中间为占位符,后期会替换处理)(SubjectSimp.html)

<foreach>
    <li>
        <span class="pro-img"><a target="_blank" href="$Url$"><img src="$ThumbnailUrl$" alt="$ProductName$"></a></span>
        <div class="pro-info">
            <div class="pro-name">
                <a href="$Url$">$ProductName$</a>
            </div>
            <div class="pro-price">
                <span><i class="rmb">€</i>$RankPrice$</span><i>€$MarketPrice$</i>
                <span>消费积分:<i>$PresentIntegral$</i></span>
            </div>
        </div>
    </li>
</foreach>

处理文件(Common_SubjectProduct_Simple.cs)

	    string url = Globals.PhysicalPath(HiContext.Current.GetSkinPath() + "/" + this.SkinName);//获取目标文件地址
            HtmlDocument htmlDocument = this.GetHtmlDocument(url);//打开目标文件
            HtmlNode node2 = htmlDocument.DocumentNode.SelectSingleNode("//foreach");//获得目标文件的节点
            string innerHtml = htmlDocument.DocumentNode.InnerHtml;//获得目标文件内容
            if (node2 != null)
            {
                sb.AppendLine(innerHtml.Substring(0, innerHtml.IndexOf("<foreach>")));
                foreach (DataRow row in tbProducts.Rows)
                {
                    string defaultProductImage = SettingsManager.GetMasterSettings().DefaultProductImage;
                    if (!string.IsNullOrEmpty(row["ThumbnailUrl" + node.Attributes["ImageSize"].Value].ToNullString()))
                    {
                        defaultProductImage = row["ThumbnailUrl" + node.Attributes["ImageSize"].Value].ToString();
                    }
                    string str4 = Globals.ApplicationPath + defaultProductImage;
                    string newValue = Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"] });
                    string str6 = row["ProductName"].ToString();
                    string str7 = string.Empty;
                    string str8 = Globals.FormatMoney(row["SalePrice"].ToDecimal() * Convert.ToDecimal(masterSettings.ExchangeRate));
                    string str10 = row["PresentIntegral"].ToString();
                    string str9 = this.IsDelayedLoading ? (" data-url=" + str4 + " ") : string.Empty;
                    if (this.IsDelayedLoading)
                    {
                        node2.InnerHtml = node2.InnerHtml.Insert((node2.InnerHtml.IndexOf("$ThumbnailUrl$") + 1) + "$ThumbnailUrl$".Length, str9);
                    }
                    sb.AppendLine(node2.InnerHtml.Replace("$Url$", newValue).Replace("$ThumbnailUrl$", this.IsDelayedLoading ? string.Empty : str4).Replace("$ProductName$", str6).Replace("$MarketPrice$", str7).Replace("$RankPrice$", str8).Replace("$PresentIntegral$", str10));
                }
                if (innerHtml.IndexOf("</foreach>") > -1)
                {
                    sb.AppendLine(innerHtml.Substring(innerHtml.IndexOf("</foreach>") + 10));
                }
            }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值