perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home »» Info Bank »» Articles » Microsoft Visual Sourcesafe 6.0 & XML Saturday, 23 February 2008
 

Back to Articles Page      

        


Microsoft Visual Sourcesafe 6.0 & XML

  • Introduction
    Visual SourceSafe version 6.0 exposes Automation interface for two purposes: Trapping SourceSafe Events (responds to certain events as they occur within a SourceSafe session) & Driving a SourceSafe Database (manage SourceSafe Database). MSXML is the Microsoft software component that provides core XML services. It is a DOM-based XML Parser, a SAX parser that is optimized for handling large documents, and it also implements XSLT and XPath. In this article, we'll use Visual SourceSafe version 6.0 Automation to read the VSS database & then using MSXML build an XML document containing information about the VSS database (projects, files, checked-out status, etc.). We'll use Visual Basic 6.0 to write a sample application for this article.


  • Getting Ready
    If you want to create an application that uses the SourceSafe Automation interface, you need to set a reference to the SourceSafe 6.0 Type Library. If this option doesn't appear in your Visual Basic Reference list, use the Browse command from the References dialog box to locate the file ssapi.dll. By default, this file will reside in the WIN32 folder of your Visual SourceSafe 6.0 installation.

    The sample application with this article uses the latest External linkMSXML 3.0 Parser, you could use the 2.5 version after a small change in the code. So make sure you have MSXML DLL present and registered on your machine. You'll also need Visual Sourcesafe 6.0 and a sample database that you can use to run the sample. We recommend reading External linkCreate a VSS database section from this article on ASPToday.


  • Details
    It's very easy to use the Visual SourceSafe Automation objects. First step is to add the reference (Project | References) to Microsoft SourceSafe 6.0 type library. Next create an object of type VSSDatabase and call it's Open method. This method takes three parameters: srcsafe.ini file name with path, username and the password. Then using VSSItem("$/") property you can get to the root node in the database and everything after that is VSSItems and VSSItem.

   
        Dim objVSSDatabase As SourceSafeTypeLib.VSSDatabase
        Dim objVSSRoot As SourceSafeTypeLib.VSSItem

        On Error Resume Next

        'Open Visual Sourcesafe database and get reference to the Root node
        Set objVSSDatabase = New SourceSafeTypeLib.VSSDatabase
        objVSSDatabase.Open txtPath.Text & "\srcsafe.ini", txtUserName.Text, txtPassword.Text

        Select Case Err
        Case 0
                'Create reference to the root
                Set objVSSRoot = objVSSDatabase.VSSItem("$/", False)
        Case Else
                MsgBox ("Error logging into SourceSafe!" + vbCrLf + Err.Description)
                Return
        End
        End Select

In our sample application, we get the path for srcsafe.ini, username and password from the user. When clicked on the Go button, we connect to the VSS database, get information about projects and files under the root database ($/) recursively and generate XML document using MSXML. Here is how sample application looks like:



Following lines create and initialize the XML document
    Dim objXMLRootElement As MSXML2.IXMLDOMElement
    Set objXMLDoc = New MSXML2.DOMDocument

    Set declPI = objXMLDoc.createProcessingInstruction("xml", " version=""1.0"" ")
    objXMLDoc.appendChild declPI

    Set objXMLRootElement = objXMLDoc.createElement("VSSProjects")
    objXMLDoc.appendChild objXMLRootElement
Next, we call our recursive function ReadVSSWriteXML, which actually iterates over all projects and files under the root VSS database, for each file/project gets attributes (like IsCheckedOut, WorkingFolder, IsDeleted, etc.) and adds an element to the XML document.
Private Sub ReadVSSWriteXML(objVSSRoot As VSSItem,ByRef objRootElement As IXMLDOMElement)
    Dim objProjectItems As SourceSafeTypeLib.IVSSItems
    Dim objCurVSSItem As SourceSafeTypeLib.VSSItem
    
    Dim objXMLNewElement As MSXML2.IXMLDOMElement
    
    Set objProjectItems = objVSSRoot.Items
    
    iProjCount = objProjectItems.Count
    For iIndex = 1 To iProjCount
        Set objCurVSSItem = objProjectItems.Item(iIndex)
        
        'If current item is a file
        If objCurVSSItem.Type = 1 Then
            Set objXMLNewElement = objXMLDoc.createElement("FILE")
            objXMLNewElement.setAttribute "Name", objCurVSSItem.Name
            
            If objCurVSSItem.Binary Then
                objXMLNewElement.setAttribute "IsBinaryFile", "true"
            Else
                objXMLNewElement.setAttribute "IsBinaryFile", "false"
            End If
            
            If objCurVSSItem.Deleted Then
                objXMLNewElement.setAttribute "IsDeleted", "true"
            Else
                objXMLNewElement.setAttribute "IsDeleted", "false"
            End If
            
            If objCurVSSItem.IsCheckedOut > 0 Then
                objXMLNewElement.setAttribute "IsCheckedOut", "true"
            Else
                objXMLNewElement.setAttribute "IsCheckedOut", "false"
            End If
        Else
            Set objXMLNewElement = objXMLDoc.createElement("PROJECT")
            objXMLNewElement.setAttribute "Name", objCurVSSItem.Name
            objXMLNewElement.setAttribute "WorkingFolder", objCurVSSItem.LocalSpec
        End If
        
        objRootElement.appendChild objXMLNewElement
        
        'If current item is a subproject
        If objCurVSSItem.Type = 0 Then
            ReadVSSWriteXML objCurVSSItem, objXMLNewElement
        End If

    Next iIndex
    
End Sub
The genrated XML is displayed in the multiline edit box below the Go button on the form. If you copy and save this XML into a .xml file and view it in Internet Explorer, you'll see somthing like:
  • Summary
    We just learned how to use Microsoft Visual Sourcesafe 6.0 Automation and MSXML together with the help of a small Visual Basic Application.
    Download source code for this article. [3KB]


  • References
    External linkVisual Sourcesafe 6.0 Automation
    perfectxml.com's MSXML Resource Pages
  •   

    Back to Articles Page      

    All information on this site is for training only. We do not warrant its correctness or its fitness to be used. The risk of using it remains entirely with the user.

     

  Contact Us |  | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy