35

我正在使用 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");
    }

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>

感谢提前,乔治

4

5 回答 5

69

猜测一下,html 包含 javascript 或其他大括号 ( {and }) 来源,它们都需要加倍 (to {{and }}) 才能与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");
于 2009-07-13T09:47:28.553 回答
20

您的模板包含{}需要转义的字符,否则它们会混淆String.Format. {{使用and转义它们}}。或者,使用不同的机制,例如String.Replace.

于 2009-07-13T09:48:40.877 回答
7

{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,您可以在模板中执行简单的循环和条件。请参阅文档

于 2009-07-13T09:56:18.303 回答
1

'模板'变量的内容是什么?

很难说你的代码有什么问题,但大概,模板变量不包含作为占位符的字符串。(例如“这是一些字符串 {0}”)。

我认为您应该利用 IDE 提供的工具:调试代码,使用手表检查模板变量的内容。

于 2009-07-13T09:47:49.770 回答
0

模板文件中有什么?

如果大括号不是 {int} 格式,或者格式语句的参数多于参数,它会抛出异常。

异常中的消息是什么?

这是你的 CSS 做的。正如其他人提到的那样,您需要进行正则表达式替换,或者一行中的一堆 String.Replace 命令将您的变量标记为 %%VARIABLE_NAME%% 并使用字符串替换来替换它们

于 2009-07-13T09:48:28.583 回答