Source Code

for file /AJAXEngine/S03_AJAXControls/TreeView.asmx

<%@ WebService Language="C#" Class="TreeView" %>
// TreeView.asmx
// WebService serving data for a AJAX TreeView Control.
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// 02.01.2006 created by Matthias Hertel

using System;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
using System.Xml;

[WebService(Namespace = TreeView.LOCALNAMESPACE,
  Description = "A WebService for getting hierarchical data for the AJAX Tree Control.")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TreeView : System.Web.Services.WebService, AJAXControls.ITreeViewDataService {

  // The namespace for the WebService and the passed xml documents.
  // "d" is always used as the the prefix in this class.
  private const string LOCALNAMESPACE = @"http://www.mathertel.de/TreeView/TreeView";
  

  /// <summary>Name of the file containing the tracking statistics.</summary>
  private const string FILENAME = @"\AjaxEngine\S03_AJAXControls\treeviewdata.xml";

  /// <summary>Name of the cache-entry for the tracking data.</summary>
  private const string FILENAMEKEY = @"mathertel.sampletreedata";


  /// <summary>Retrieve the sub nodes of a specific folder.</summary>
  /// <param name="path">a path to a folder.</param>
  /// <returns>A XML document (tree) containing folder and file nodes.</returns>
  [WebMethod(Description = "Get the subnodes of a given folder path.")]
  public XmlNode GetSubNodes(string path) {
    XmlNode xRet = null;
    
    if (path == null)
      throw new ArgumentNullException("path");
    if (! path.StartsWith("/"))
      throw new ArgumentException("no valid path was given.", "path");
    
    // load the whole document
    XmlDocument xDoc = Load();

    // build a correct XPath expression
    StringBuilder xpath = new StringBuilder();
    xpath.Append("/tree");
    if (path.Length > 1) {
      foreach (string p in path.Substring(1).Split('/')) {
        xpath.AppendFormat("/folder[@name='{0}']", p);
      }
    }
    
    // find the node pecified by the path
    XmlElement x = xDoc.SelectSingleNode(xpath.ToString()) as XmlElement;

    // do a shallow copy
    xRet = xDoc.CreateElement("tree");
    foreach (XmlNode xe in x.ChildNodes) {
      xRet.AppendChild(xe.CloneNode(false));
    } // foreach
   
    // return the copy
    return (xRet);
  } // GetSubNodes


  #region Load and Save

  /// <summary>Load current data from the global cache or the file.</summary>
  private static XmlDocument Load() {
    Cache c = HttpRuntime.Cache;
    XmlDocument xDoc = (XmlDocument)c[FILENAMEKEY];

    if (xDoc == null) {
      string fileName = HttpRuntime.AppDomainAppPath + FILENAME;
      xDoc = new XmlDocument();

      xDoc.Load(fileName);

      c.Insert(FILENAMEKEY, xDoc, new CacheDependency(fileName));
    } // if
    return (xDoc);
  } // Load


  /// <summary>Save current data to the cache and file.</summary>
  /// <param name="XmlDoc">The xml document to be saved.</param>
  private static void Save(XmlDocument XmlDoc) {
    string fileName = HttpRuntime.AppDomainAppPath + FILENAME;
    if (XmlDoc != null) {
      try {
        XmlDoc.Save(fileName);
      } catch (Exception) { } // try
      HttpRuntime.Cache.Insert(FILENAMEKEY, XmlDoc, new CacheDependency(fileName, DateTime.Now.AddSeconds(1)));
    } // if
  } // Save


  #endregion


} // class



This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.