|
// Disclaimer and Copyright Information
// ADSIObject.cs :
//
// All rights reserved.
//
// Written by Pardesi Services, LLC
// Version 1.0
//
// Distribute freely, except: don't remove our name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// softomatix@pardesiservices.com
///////////////////////////////////////////////////////////////////////////////
//
using System;
using System.Diagnostics;
using System.Text;
using System.DirectoryServices;
using System.Xml;
namespace ASPNet_App
{
/// <summary>
///
/// </summary>
public abstract class ADSIObject
{
protected bool m_bInitialized = false;
protected int m_nNumProperties = -1;
protected XmlDocument m_obPropertiesDoc = null;
public ADSIObject()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
///
/// </summary>
/// <param name="propColl"></param>
/// <returns></returns>
public virtual bool Initialize(ResultPropertyCollection propColl)
{
// Check if the input collection object is null or not.
if (null == propColl)
{
return false;
}
XmlNode obTopNode;
try
{
// Create XMl Document to store values.
m_obPropertiesDoc = new XmlDocument();
obTopNode = this.AppendTopLevelNode();
if (null == obTopNode)
{
Trace.WriteLine("Failed to add top level node to xml document");
return false;
}
// Iterate over each key in the collection.
foreach (string strKey in propColl.PropertyNames)
{
foreach (object obProp in propColl[strKey])
{
this.AppendPropertyNode(obTopNode, strKey, obProp);
}
}
// Process the XML Document to store the information in local variables.
this.ProcessUserData();
this.m_nNumProperties = propColl.PropertyNames.Count;
this.m_bInitialized = true;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return false;
}
return true;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public XmlDocument GetProperties()
{
if (!m_bInitialized)
{
return null;
}
return m_obPropertiesDoc;
}
/// <summary>
///
/// </summary>
public int NumProperties
{
get
{
return m_nNumProperties;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected virtual XmlNode AppendTopLevelNode()
{
XmlNode obTopNode = null;
if (this.m_obPropertiesDoc == null)
{
return null;
}
StringBuilder strTopNode = new StringBuilder();
strTopNode.Append ("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
strTopNode.Append ("<adsi_object> </adsi_object>");
try
{
m_obPropertiesDoc.LoadXml(strTopNode.ToString());
obTopNode = m_obPropertiesDoc.SelectSingleNode("//adsi_object");
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return null;
}
return obTopNode;
}
/// <summary>
///
/// </summary>
/// <param name="strNode"></param>
/// <param name="prop"></param>
/// <returns></returns>
protected bool AppendPropertyNode(XmlNode node, string strNode, object prop)
{
try
{
XmlElement elem = this.m_obPropertiesDoc.CreateElement(strNode);
XmlAttribute attrib = this.m_obPropertiesDoc.CreateAttribute("type");
attrib.InnerText = prop.GetType().ToString();
// Append the value type attribute to element.
elem.Attributes.Append(attrib);
// Append the value now...
elem.InnerText = prop.ToString();
// Append this element to top node.
node.AppendChild(elem);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
return true;
}
/// <summary>
///
/// </summary>
protected virtual void ProcessUserData()
{
}
/// <summary>
///
/// </summary>
/// <param name="strQuery"></param>
/// <param name="strVal"></param>
protected void GetNodeValue(string strQuery, ref string strVal)
{
try
{
XmlNode node = this.m_obPropertiesDoc.SelectSingleNode(strQuery);
strVal = node.InnerText;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.