Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#/ASP.NET中呈现html的最有效方法_C#_Asp.net - Fatal编程技术网

在C#/ASP.NET中呈现html的最有效方法

在C#/ASP.NET中呈现html的最有效方法,c#,asp.net,C#,Asp.net,我们必须在C#中实现一些html呈现,我们正在寻找有效的方法来实现它 下面是我们要呈现的html: <h1> Title {34}</h1> <p>Paragraph {4}</p> <div> Div here {14}</div> 标题{34} 第{4}段 这里是Div{14} 必须用字典中的值替换数字{34}、{4}、{14} 我们正在寻找有效的查找、提取号码和替换。html也包含动态和数字 一个解决方案是使用正

我们必须在C#中实现一些html呈现,我们正在寻找有效的方法来实现它

下面是我们要呈现的html:

<h1> Title {34}</h1>
<p>Paragraph  {4}</p>
<div> Div here {14}</div>
标题{34}
第{4}段

这里是Div{14}
必须用字典中的值替换数字{34}、{4}、{14}

我们正在寻找有效的查找、提取号码和替换。html也包含动态和数字


一个解决方案是使用正则表达式,但我们有更好的替代方法吗?

我的理解是,您有一个从其他地方获取的静态html文件,您希望将模式
{x}
替换为
dict[x]
,其中x是一个数字,是字典值的键。这可以通过regex实现,服务器端使用C#(或者其他任何东西),客户端使用Javascript。下面是一个示例js解决方案:

constoldhtml=“Title{34}段落{4}

Div here{14}” document.querySelector('#before')。innerHTML=oldHTML; 常量dict={ “4”:“4的dict值”, “14”:“14的dict值”, “34”:“34的dict值” }; const newHTML=oldHTML.replace(/\{[0-9]+\}/g,(str)=>{ const num=str.slice(1,str.length-1); 返回dict[num]; }); document.querySelector(“#after”).innerHTML=newHTML
我做了一些研究,发现了一些我想分享的东西。请注意,呈现html是包含
{num}

下面是C#中的代码:

以下是JavaScript版本的答案:

constoldhtml=“Title{34}段落{4}

Div here{14}”; document.querySelector('#oldHTML')。innerHTML=oldHTML; 常量dict={ “4”:“4的dict值”, “14”:“14的dict值”, “34”:“34的dict值” }; 让start=performance.now(); 让newHTML=oldHTML.replace(/\{[0-9]+\}/g,(str)=>{ const num=str.slice(1,str.length-1); 返回dict[num]; }); var end=performance.now(); log(“正则表达式的执行时间为”+(结束-开始)+“毫秒”); document.querySelector('#newHtmlRegex')。innerHTML=newHTML; 开始=性能。现在(); 让某人=[]; var str=oldHTML.split(/{}/); 如果(str.length>0){ 对于(变量i=0;ii+1) 某人推(dict[str[i+1]]); } } newHTML=sb.join(“”); end=performance.now(); log(“执行时间分割花费了”+(结束-开始)+“毫秒”); document.querySelector('#newHtmlSplit')。innerHTML=newHTML
我将为您提供一种解决方案,它不仅比拆分更快,而且内存效率更高,适合于流操作

以下是c代码(我将您的解决方案与我的解决方案放在一起,以便比较运行时间):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统文本;
使用System.Text.RegularExpressions;
使用系统诊断;
名称空间htmlRender
{
班级计划
{
const string oldHTML=“Title{34}段落{4}

Div here{14}”; 静态字典dict=新字典() { {“4”,“4”的dict值}, {“14”,“14”的dict值}, {“34”,“34”的dict值} }; 静态void Main(字符串[]参数) { 秒表sw=Stopwatch.StartNew(); 正则表达式rx=新正则表达式(@“{\d+}”); 字符串newHTMLRegex=rx.Replace(oldHTML,newmatchevaluator(ReplaceText)); sw.Stop(); //Console.WriteLine(newHTMLRegex); WriteLine(“正则表达式的执行时间为”+sw.ElapsedTicks+“ticks”); sw=秒表。开始新(); var sb=新的StringBuilder(); var str=oldHTML.Split(新字符[]{{{'',}}); 如果(str.Length>0) { 对于(int i=0;ii+1) 某人追加(dict[str[i+1]]); } } 字符串newHTMLSplit=sb.ToString(); sw.Stop(); //Console.WriteLine(newHTMLSplit); WriteLine(“执行时间分割花费”+sw.ElapsedTicks+“ticks”); sw=秒表。开始新(); var charBuffer=新字符[10]; int缓冲=0; 布尔缓冲=假; var writer=新的StringBuilder(); for(int i=0;iusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text; using System.Text.RegularExpressions; using System.Diagnostics; namespace htmlRender { class Program { const string oldHTML = "<h1>Title {34}</h1><p>Paragraph {4}</p><div> Div here {14}</div>"; static Dictionary<string, string> dict = new Dictionary<string, string>() { { "4", "dict value for 4" }, { "14", "dict value for 14"}, { "34", "dict value for 34"} }; static void Main(string[] args) { Stopwatch sw = Stopwatch.StartNew(); Regex rx = new Regex(@"{\d+}"); string newHTMLRegex = rx.Replace(oldHTML, new MatchEvaluator(ReplaceText)); sw.Stop(); //Console.WriteLine(newHTMLRegex); Console.WriteLine("Execution time regex took " + sw.ElapsedTicks + " ticks."); sw = Stopwatch.StartNew(); var sb = new StringBuilder(); var str = oldHTML.Split(new char[] { '{', '}' }); if (str.Length > 0) { for (int i = 0; i < str.Length; i += 2) { sb.Append(str[i]); if (str.Length > i + 1) sb.Append(dict[str[i + 1]]); } } string newHTMLSplit = sb.ToString(); sw.Stop(); //Console.WriteLine(newHTMLSplit); Console.WriteLine("Execution time split took " + sw.ElapsedTicks + " ticks."); Console.ReadKey(); } static string ReplaceText(Match m) { string x = m.ToString().Replace("{", "").Replace("}", ""); return dict[x]; } } }
Execution time regex took 6894 ticks.
Execution time split took 65 ticks.
Execution time regex took 17814 ticks.
Execution time split took 100 ticks.
Execution time stream took 51 ticks.