当格式字符串包含“{”的String.Format例外
-
12-09-2019 - |
题
我使用VSTS 2008 + C#+ NET 2.0。当执行下面的语句,有出现FormatException从的String.Format声明抛出,任何想法有什么不好?
下面是从哪里得到我使用template.html。我想格式化该部分M = {0}在template.html。
string template = String.Empty;
using (StreamReader textFile = new StreamReader("template.html"))
{
template = textFile.ReadToEnd();
String.Format(template, "video.wmv");
}
http://www.mediafire.com/download.php?u4myvhbmmzg
EDIT 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>
在avdance感谢, 乔治
解决方案
目前猜测,在html包含JavaScript或括号({
和}
)的另一来源这将所有需要加倍(以{{
和}}
)为与string.Format
可用。我期望不同的(更明显)令牌可以是为了,即%%FILENAME%%
。然后使用正则表达式或string.Replace
。
如果你有一个单一的标签,string.Replace
是细;如果你有很多,也有与正则表达式和MatchEvaluator
技巧,可能是有益的 - 像这样但使用不同的正则表达式。
的示例HTML加入后
更新:我肯定会使用不同的令牌;在最基本的级别:
<param name="initParams" value="cc=true,markers=true,m=%%FILENAME%%" />
和
template = template.Replace("%%FILENAME%%", "video.wmv");
其他提示
您的模板包含需要转义{
和}
字符,否则他们混淆String.Format
。使用{{
和}}
逃离他们。可替代地,使用不同的机制,例如String.Replace
。
的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());
在template.html
文件更改单个行:
从:
<param name="initParams" value="cc=true,markers=true,m={0}" />
为:
<param name="initParams" value="cc=true,markers=true,m=$content$" />
当模板引擎遇到在模板$content$
,它与“内容”属性所设置使用代码的值替换它。
使用StringTemplate的,您可以在模板中做简单的循环和条件。请参阅的文档。
笏是“模板”变量的内容?
这很难说有什么不对你的代码,但据推测,模板变量不包含一个字符串,它作为一个占位符。 (如 “这是一些串{0}”)。
我认为你应该利用你的IDE提供的工具:调试代码,利用手表来检查模板变量的内容
什么在模板文件?
如果有大括号不属于格式{} INT的或有超过有争论的格式声明,它会抛出异常。
什么是在异常的讯息?
这是你的CSS多数民众赞成这样做。作为somoene别人提到你需要DOA正则表达式替换,或一堆的与string.replace一排标记命令你的变量与%% VARIABLE_NAME %%和使用字符串替换来替换它们。