我使用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

笏是“模板”变量的内容?

这很难说有什么不对你的代码,但据推测,模板变量不包含一个字符串,它作为一个占位符。 (如 “这是一些串{0}”)。

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

什么在模板文件?

如果有大括号不属于格式{} INT的或有超过有争论的格式声明,它会抛出异常。

什么是在异常的讯息?

这是你的CSS多数民众赞成这样做。作为somoene别人提到你需要DOA正则表达式替换,或一堆的与string.replace一排标记命令你的变量与%% VARIABLE_NAME %%和使用字符串替换来替换它们。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top