Geo*_*ge2 31 .net c# string.format
我使用的是VSTS 2008 + C#+ .Net 2.0.当执行以下语句时,会从String.Format语句中抛出FormatException,任何想法有什么问题?
这是获取我正在使用的template.html的地方.我想在template.html中格式化此部分m = {0}.
string template = String.Empty;
using (StreamReader textFile = new StreamReader("template.html"))
{
template = textFile.ReadToEnd();
String.Format(template, "video.wmv");
}
Run Code Online (Sandbox Code Playgroud)
http://www.mediafire.com/download.php?u4myvhbmmzg
编辑1:
这是我的template.html的内容,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
<title>Silverlight Project Test Page </title>
<style type="text/css">
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost {
height: 100%;
}
</style>
<script type="text/javascript">
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n" ;
errMsg += "Code: "+ iErrorCode + " \n";
errMsg += "Category: " + errorType + " \n";
errMsg += "Message: " + args.ErrorMessage + " \n";
if (errorType == "ParserError")
{
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError")
{
if (args.lineNumber != 0)
{
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
throw new Error(errMsg);
}
</script>
</head>
<body>
<!-- Runtime errors from Silverlight will be displayed here.
This will contain debugging information and should be removed or hidden when debugging is completed -->
<div id='errorLocation' style="font-size: small;color: Gray;"></div>
<div id="silverlightControlHost">
<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="500" height="240">
<param name="source" value="ClientBin/VideoPlayer.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="initParams" value="cc=true,markers=true,m={0}" />
<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>
<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
感谢avvance,乔治
Mar*_*ell 60
猜测,html包含javascript或其他大括号({和}),它们都需要加倍({{和}})才能使用string.Format.我期望一个不同的(更明显的)令牌可能是有序的,即%%FILENAME%%.然后使用正则表达式或string.Replace.
如果你有一个标签,string.Replace很好; 如果你有很多,有正则表达式的技巧,MatchEvaluator这可能会有所帮助 - 就像这样,但具有不同的正则表达式模式.
添加示例html后更新:我肯定会使用不同的令牌; 在最基本的层面:
<param name="initParams" value="cc=true,markers=true,m=%%FILENAME%%" />
Run Code Online (Sandbox Code Playgroud)
和
template = template.Replace("%%FILENAME%%", "video.wmv");
Run Code Online (Sandbox Code Playgroud)
的String.format()不处理{,并}在格式字符串.您需要更换{与{{和}与}}无处不在的template.html文件.除了使用{0}占位符的单个位置.
不是很优雅.
相反,请考虑使用模板引擎.有关建议,请参见http://csharp-source.net/open-source/template-engines.
下一个最佳解决方案是使用正则表达式(使用MatchEvaluator)或string.Replace(),如其他答案所示.
编辑:
以下是使用StringTemplate模板引擎的示例:
StringTemplate htmlpage = new StringTemplate(File.ReadAllText("template.html"));
htmlpage.SetAttribute("content", "video.wmv");
Console.WriteLine(htmlpage.ToString());
Run Code Online (Sandbox Code Playgroud)
更改template.html文件中的一行:
从:
<param name="initParams" value="cc=true,markers=true,m={0}" />
Run Code Online (Sandbox Code Playgroud)
至:
<param name="initParams" value="cc=true,markers=true,m=$content$" />
Run Code Online (Sandbox Code Playgroud)
当模板引擎$content$在模板中遇到时,它会将其替换为您使用代码设置的"content"属性的值.
使用StringTemplate,您可以在模板中执行简单的循环和条件.请参阅文档.
| 归档时间: |
|
| 查看次数: |
31438 次 |
| 最近记录: |