开发者

Problem escaping string

const string postdescWithComments = @"<span class=""footerLeft"">posted on <a href=""{0}"" title = ""Permanent link to this post"">{1}</a></span><span class=""footerRight""><a href=""开发者_如何学Python{2}#feedback"" title=""comments, pingbacks, trackbacks"" class=""comments""><img class=""feedbackimage"" src=""/skins/xxx/images/cloud.gif"" width=""13"" height=""12"" border=""0"" />Comments({3})</a><a href=""{0}"" class=""moreimage"" title = ""Permanent link to this post""><img src="" 
+ <%=WebUtilityMethods.GetRoot()%> + 
""skins/xxx/images/document.gif border=""0""> More</a></span>";

I can't seem to escape this correctly and it's driving me nuts.

GetRoot() just returns a string


I find it easier to follow using String.Format. Try this:

String.Format("<img src=\"{0}skins/xxx/images/document.gif\" border=\"0\">", WebUtilityMethods.GetRoot());


Why are you trying to escape it? That code should run inside ASP.NET using declarative syntax, you shouldn't need to escape anything, or perhaps I'm missing something? Try this:

<img src=' 
    <%=WebUtilityMethods.GetRoot() + 
    "skins/xxx/images/document.gif"  %>'
    border="0">

EDIT: that was the original question. Here's for the new challenge (question was completely changed). It did not contain any errors, but it was hard to read with all the "" so I replaced it. Not sure what you're after, seems to me that you are trying to use interpreted declarative syntax inside your code behind, which is not possible, sorry:

// slightly more readable:
const string postdescWithComments =
                @"
<span class='footerLeft'>
    posted on <a href='{0}' title = 'Permanent link to this post'>{1}</a>
</span>
<span class='footerRight'>
    <a href='{2}#feedback' 
        title='comments, pingbacks, trackbacks' 
        class='comments'>

        <img class='feedbackimage' 
            src='/skins/pmall/images/cloud.gif' width='13' 
            height='12' border='0' />

        Comments({3})
    </a>

    <a href='{0}' class='moreimage' title = 'Permanent link to this post'>
        <img src='{4}skins/xxx/images/document.gif' border='0'>
        More
    </a>
</span>";

// somewhere else, this is where your GetRoot() goes:
string parsedString = String.Format(
     postdescWithComments, 
     href, 
     hrefText,
     hrefFeedback, 
     feedbackText,
     WebUtilityMethods.GetRoot());

EDIT: as you've found out, it is very hard to check code like that (esp. when it is on one string as in your original post) for errors. If possible, move the code to the declarative section (the part where the HTML of your page is) Then you can use the <%= syntax and as a bonus you get an automatic syntax-check on the HTML.

If that's not possible, you should replace the <%=...%> part with a {4} and use String.Format to fill in the blanks, the same way you did with the other parts.

EDIT: this last remark, and the well-spotted (!) closing quote from Agent_9191 have been reflected in the code block for clarity.


The other answers were close in stating it's easier to escape your Image tag, but really you were missing a closing quote for the source. It's also easier to nest altering quotes rather than trying to escape quotes. Try this:

const string postdescWithComments = @"<span class='footerLeft'>posted on <a href='{0}' title='Permanent link to this post'>{1}</a></span><span class='footerRight'><a href='{2}#feedback' title='comments, pingbacks, trackbacks' class='comments'><img class='feedbackimage' src='/skins/xxx/images/cloud.gif' width='13' height='12' border='0' />Comments({3})</a><a href='{0}' class='moreimage' title = 'Permanent link to this post'><img src='"" + <%=WebUtilityMethods.GetRoot()%> + ""skins/xxx/images/document.gif' border='0' /> More</a></span>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜