Get Single XML Element - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Get Single XML Element

Demo Code


using System.Xml.Serialization;
using System.IO;//  w  ww.  j  a v  a 2 s  .co  m
using System.Xml;
using System.Text.RegularExpressions;
using System;

public class Main{
        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("Expected single element '{0}', got multiple ({1})", name, list.Count));
         }
         return (XmlElement) list.Item(0);
      }
}

Related Tutorials