开发者

Purify Embedding YouTube Videos method in C#

How would look a method in C# that "purify" an embedded YouTube video markup?

So method input would be:

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/gtNlQodFMi8&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/gtNlQodFMi8&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>
开发者_开发技巧

Output:

<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/gtNlQodFMi8">
<param name="movie" value="http://www.youtube.com/v/gtNlQodFMi8" />

YouTube embedded video markup is problematic because of the inline style (width, height) and it is not XHTML valid.


Well you could always write a C# method that will output the code that you want given a certain input, in this case the XML of the object, then parse it and pull out the bits you want and construct your code and output it, then from the aspx page you simply call it with server code, like this

<% MyYoutubeUtils.ShowEmebddedVideo("<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/gtNlQodFMi8&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/gtNlQodFMi8&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>") %>

or something like that.

Ok I am not 100% sure of the syntax, but this should give you a start.

public static string ShowEmbeddedVideo(string youtubeObject)
{
    var xdoc = XDocument.Parse(youtubeObject);
    var returnObject = string.Format("<object type=\"{0}\" data=\{1}\"><param name=\"movie\" value=\"{1}\" />",
        xdoc.Root.Element("embed").Attribute("type").Value,
        xdoc.Root.Element("embed").Attribute("src").Value);
    return returnObject;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜