Gets the single XML element. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Gets the single XML element.

Demo Code


using System.Xml.Serialization;
using System.IO;/* w  w  w  .j ava  2  s.com*/
using System.Xml;
using System.Text.RegularExpressions;
using System;

public class Main{
        /// <summary>
        /// Gets the single element.   
        /// </summary>
        /// <param name="doc">The doc.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <remarks></remarks>
      public static XmlElement GetSingleElement(XmlDocument doc, string name)
      {
         XmlNodeList list = doc.GetElementsByTagName(name);
         if (list == null)
         {
            return null;
         }
         if (list.Count > 1)
         {
            throw new CruiseControlException(string.Format(System.Globalization.CultureInfo.CurrentCulture,"Expected single element '{0}', got multiple ({1})", name, list.Count));
         }
         return (XmlElement) list.Item(0);
      }
}

Related Tutorials