Is XML Property Defined - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Is XML Property Defined

Demo Code


using System.Xml;
using System.Runtime.Remoting.Messaging;
using System.Linq;
using System;// w  w w.  ja v a2 s.  com

public class Main{
        public static bool IsPropertyDefined(this XmlElement element, string propertyName)
    {
      var attrName = propertyName.ToCamelCase();

      return (element.IsAttributeDefined(attrName) || element.HasChildElement(attrName));
    }
        public static bool HasChildElement(this XmlElement element, string childElementName)
    {
      //var hasChildElement = element.ChildNodes.Cast<XmlNode>()
      //  .Any(node => node.NodeType == XmlNodeType.Element && node.LocalName == childElementName);

      foreach (var childNode in element.ChildNodes.Cast<XmlNode>())
      {
        if ((childNode.NodeType == XmlNodeType.Element && childNode.LocalName == childElementName) || HasChildElement((XmlElement)childNode, childElementName))
        {
          return true;
        }
      }

      return false;
    }
        /// <summary>Determines whether [is attribute defined] [the specified element].</summary>
    /// <param name="element">The element.</param>
    /// <param name="attributeName">Name of the attribute.</param>
    /// <returns><c>true</c> if [is attribute defined] [the specified element]; otherwise, <c>false</c>.</returns>
    public static bool IsAttributeDefined(this XmlElement element, string attributeName)
    {
      var value = element.GetAttribute(attributeName);
      return !string.IsNullOrWhiteSpace(value);
    }
}

Related Tutorials