tags:

views:

198

answers:

1

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&amp;hl=en&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/gtNlQodFMi8&amp;hl=en&amp;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"&gt;
<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.

A: 

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&amp;hl=en&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/gtNlQodFMi8&amp;hl=en&amp;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;
}
Ryk
Yeah, but the question is exactly about that method you talk about ...
Peter Stegnar
Is the edited version more understandable?
Ryk