开发者

Purify Embedding YouTube Videos method in C#

开发者 https://www.devze.com 2022-12-25 07:13 出处:网络
How would look a method in C# that \"purify\" an embedded YouTube video markup? So method input would be:

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

精彩评论

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