Get attribute from XmlNode

image_pdfimage_print
   
 

//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license

#region using
using System;
using System.Xml;
#endregion

namespace DbmlManager.Lib.Utility
{
  #region Class Docs
  /// <summary>
  /// Summary description for XmlUtil.
  /// </summary>
  #endregion

  public class XmlUtil
  {


    #region GetAttrib(XmlNode node, string name)
    public static string GetAttrib(XmlNode node, string name)
    {
      if (node.Attributes == null)
        return null;

      if (node.Attributes[name] == null)
        return null;

      return node.Attributes[name].InnerText;
    }
    #endregion

  }
}

   
     


Get integer value from xml element

image_pdfimage_print
   
 


//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license

#region using
using System;
using System.Xml;
#endregion

namespace DbmlManager.Lib.Utility
{
  #region Class Docs
  /// <summary>
  /// Summary description for XmlUtil.
  /// </summary>
  #endregion

  public class XmlUtil
  {


    #region LoadInt(ref int val, XmlNode parent, string nodeName)
    public static void LoadInt(ref int val, XmlNode parent, string nodeName)
    {
      try
      {
        XmlElement elem = parent[nodeName];
        if (elem != null)
          val = Int32.Parse(elem.InnerText.Trim());
      }
      catch
      {
        val = 0;
      }
    }
    #endregion

  }
}

   
     


Load boolean value from xml element

image_pdfimage_print
   
 

//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license

#region using
using System;
using System.Xml;
#endregion

namespace DbmlManager.Lib.Utility
{
  #region Class Docs
  /// <summary>
  /// Summary description for XmlUtil.
  /// </summary>
  #endregion

  public class XmlUtil
  {

    #region LoadBool(ref bool val, XmlNode parent, string nodeName)
    public static void LoadBool(ref bool val, XmlNode parent, string nodeName)
    {
      try
      {
        XmlElement elem = parent[nodeName];
        if (elem != null)
          val = (elem.InnerText.Trim() == "1");
      }
      catch
      {
        val = false;
      }
    }
    #endregion

  }
}

   
     


Load String from xml element

image_pdfimage_print
   
 
//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license

#region using
using System;
using System.Xml;
#endregion

namespace DbmlManager.Lib.Utility
{
  #region Class Docs
  /// <summary>
  /// Summary description for XmlUtil.
  /// </summary>
  #endregion

  public class XmlUtil
  {


    #region LoadString(ref string val, XmlNode parent, string nodeName)
    public static void LoadString(ref string val, XmlNode parent, string nodeName)
    {
      try
      {
        XmlElement elem = parent[nodeName];
        if (elem != null)
          val = elem.InnerText.Trim();
      }
      catch
      {
        val = string.Empty;
      }
    }
    #endregion

  }
}

   
     


Create Xml Document, Node

image_pdfimage_print
   
 
//GNU General Public License version 2 (GPLv2)
//http://cbasetest.codeplex.com/license
namespace SDFL.Helper
{
    using System;
    using System.Text;
    using System.Xml;

    public class XMLHelper
    {
        public static XmlDocument CreateXmlDocument()
        {
            return CreateXmlDocument(new Version(1, 0), Encoding.UTF8);
        }

        public static XmlDocument CreateXmlDocument(Version version, Encoding encoding)
        {
            XmlDocument document = new XmlDocument();
            document.AppendChild(document.CreateXmlDeclaration(version.ToString(), encoding.BodyName, string.Empty));
            return document;
        }

        public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name)
        {
            return CreateXmlNode(xmlDocument, name, string.Empty);
        }

        public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name, string value)
        {
            XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, name, string.Empty);
            node.AppendChild(xmlDocument.CreateTextNode(value));
            return node;
        }
    }
}

   
     


DOM feature check

image_pdfimage_print
   
 
using System;
using System.Xml;

class DomFeatureChecker {

  private static readonly string [] versions = new string [] {
    "1.0", "2.0" };
  
  private static readonly string [] features = new string [] { 
    "Core", "XML", "HTML", "Views", "Stylesheets", "CSS", 
    "CSS2", "Events", "UIEvents", "MouseEvents", "MutationEvents", 
    "HTMLEvents", "Range", "Traversal" };
  
  public static void Main(string[] args) {
    XmlImplementation impl = new XmlImplementation();
  
    foreach (string version in versions) {
      foreach (string feature in features) {
        Console.WriteLine("{0} {1}={2}", feature, version, 
          impl.HasFeature(feature, null));
      }
    }
  }
}


           
         
     


Find a single Attribute of the type 'attributeType' from the supplied class

image_pdfimage_print
   
 

/*
 * Copyright  2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;

namespace Spring.Util
{
    /// <summary>
    /// General utility methods for working with annotations
    /// </summary>
    public class AttributeUtils
    {
        /// <summary>
        /// Find a single Attribute of the type &#039;attributeType&#039; from the supplied class, 
        /// traversing it interfaces and super classes if no attribute can be found on the 
        /// class iteslf. 
        /// </summary>
        /// <remarks>
        /// This method explicitly handles class-level attributes which are not declared as
        /// inherited as well as attributes on interfaces.
        /// </remarks>
        /// <param name="type">The class to look for attributes on .</param>
        /// <param name="attributeType">Type of the attribibute to look for.</param>
        /// <returns>the attribute of the given type found, or <code>null</code></returns>
        public static Attribute FindAttribute(Type type, Type attributeType)
        {
            Attribute[] attributes = Attribute.GetCustomAttributes(type, attributeType, false);  // we will traverse hierarchy ourselves.
            if (attributes.Length > 0)
            {
                return attributes[0];
            }
            foreach (Type interfaceType in type.GetInterfaces())
            {
                Attribute attrib = FindAttribute(interfaceType, attributeType);
                if (attrib != null)
                {
                    return attrib;
                }
            }
            if (type.BaseType == null)
            {
                return null;
            }
            return FindAttribute(type.BaseType, attributeType);
        }
    }
}