Removing Playlist Entries

Playlist files can be edited programmatically from a locally stored file or while a client is actively streaming content. You can modify playlist files by using the server object model and the XML Document Object Model (DOM). The following examples illustrate how to traverse the various nodes in a playlist file, removing specific nodes as they are found.

Visual Basic .NET Example

Imports Microsoft.WindowsMediaServices.Interop
Imports interop_msxml

Private Sub RemoveEntry()

  ' Declare variables.
  Dim Server As WMSServer
  Dim Playlist As IXMLDOMDocument
  Dim NodeList As IXMLDOMNodeList
  Dim Root As IXMLDOMNode
  Dim Node As IXMLDOMNode
  Dim i As Integer

  Try
    ' Create the WMSServer object.
    Server = CreateObject("wmsserver.server")

    ' Create a new playlist.
    Playlist = Server.CreatePlaylist

    ' Load an existing playlist.
    Playlist.load("c:\wmpub\wmroot\playlist.wsx")

    ' Retrieve a list of all the nodes in the file.
    NodeList = Playlist.getElementsByTagName("*")

    ' Retrieve the name of each node and remove all
    ' media elements.
    For i = 0 To NodeList.length - 1

      ' Retrieve the next node in the list.
      Node = NodeList.item(i)

      ' If the node is the first media element, then remove it.
      If Node.nodeName = "media" Then

        ' Retrieve the parent node of this media element.
        Root = Node.parentNode

        ' Remove the media element.
        Root.removeChild(Node)
        Exit For

      End If
    Next

    ' Save the playlist.
    Playlist.save("c:\wmpub\wmroot\playlist.wsx")
    Exit Sub

  Catch Err As Exception
    ' TODO: Exception handler goes here.

  Finally
    ' TODO: Clean-up code goes here.

  End Try

End Sub

C# Example

using Microsoft.WindowsMediaServices.Interop;
using interop_msxml;

// Declare variables.
WMSServer Server;
IXMLDOMDocument Playlist;
IXMLDOMNodeList NodeList;
IXMLDOMNode Root;
IXMLDOMNode Node;
int i;

try
{
  // Create the WMSServer object.
  Server = new WMSServerClass();

  // Create a new playlist.
  Playlist = Server.CreatePlaylist();

  // Load an existing playlist.
  Playlist.load("c:\\wmpub\\wmroot\\playlist.wsx");

  // Retrieve a list of all the nodes in the file.
  NodeList = Playlist.getElementsByTagName("*");

  // Retrieve the name of each node and remove the first
  // media element.
  for (i=0; i< NodeList.length; i++)
  {
    // Retrieve the next node in the list.
    Node = NodeList[i];

    // If the node is the first media element, then remove it.
    if (Node.nodeName == "media")
    {
      // Retrieve the parent node of this media element.
      Root = Node.parentNode;

      // Remove the media element.
      Root.removeChild(Node);
      break;
    }
  }

  // Save the playlist.
  Playlist.save("c:\\wmpub\\wmroot\\playlist.wsx");
}

catch (Exception)
{
  // TODO: Exception handler goes here.
}

finally
{
  // TODO: Clean-up code goes here.
}

C++ Example

// Include header files.
#include <windows.h>
#include <atlbase.h>    // Includes CComBSTR and CComVariant.
#include "wmsserver.h"

// Declare variables and interfaces.
IWMSServer      *pServer;
IXMLDOMDocument *pPlaylist;
IXMLDOMNode     *pRoot;
IXMLDOMNode     *pNode;
IXMLDOMNodeList *pNodeList;

HRESULT         hr;
VARIANT_BOOL    bVal;
CComVariant     varFile;
CComBSTR        bstrName;
long            lCount;

// Initialize the COM library and retrieve a pointer
// to an IWMSServer interface.
hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_WMSServer,
                      NULL,
                      CLSCTX_ALL,
                      IID_IWMSServer,
                      (void **)&pServer);
if (FAILED(hr)) goto EXIT;

// Create a new playlist.
hr = pServer->CreatePlaylist(&pPlaylist);
if (FAILED(hr)) goto EXIT;

// Load an existing playlist.
varFile = "c:\\wmpub\\wmroot\\playlist.wsx";
hr = pPlaylist->load(varFile, &bVal);
if (FAILED(hr)) goto EXIT;

// Retrieve a list of all the nodes in the file.
bstrName = "*";
hr = pPlaylist->getElementsByTagName(bstrName, &pNodeList);
if (FAILED(hr)) goto EXIT;

// Retrieve the total count of nodes.
hr = pNodeList->get_length(&lCount);
if (FAILED(hr)) goto EXIT;

// Retrieve the name of each node and remove all media elements.
for(long i = 0; i < lCount; i++)
{
    // Retrieve the next node in the list.
    hr = pNodeList->get_item(i, &pNode);
    if (FAILED(hr)) goto EXIT;

    // Retrieve the name of the node.
    hr = pNode->get_nodeName(&bstrName);
    if (FAILED(hr)) goto EXIT;

    // If the node is a media element, then remove it.
    if(_wcsicmp(bstrName, L"media") == 0)
    {
        // Retrieve the parent node of this media element.
        pNode->get_parentNode(&pRoot);

        // Remove the media element.
        pRoot->removeChild(pNode, NULL);  

        // Release the IXMLDOMNode object.
        pRoot->Release();          
    }

    // Release the IXMLDOMNode object.
    pNode->Release();
}

// Save the playlist.
varFile = "file://c:\\wmpub\\wmroot\\playlist.wsx";
hr = pPlaylist->save(varFile);
if (FAILED(hr)) goto EXIT;

EXIT:
    // TODO: Release temporary COM objects and uninitialize COM.

See Also (General)

See Also (Visual Basic .NET)

See Also (C#)

See Also (C++)