Selects the required value from XML Document via xpath. - CSharp System.Xml

CSharp examples for System.Xml:XML Document

Description

Selects the required value from XML Document via xpath.

Demo Code


using System.Xml.Serialization;
using System.IO;/*from ww  w. ja v a  2s . c o m*/
using System.Xml;
using System.Text.RegularExpressions;
using System;

public class Main{
        /// <summary>
        /// Selects the required value.   
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <param name="xpath">The xpath.</param>
        /// <returns></returns>
        /// <remarks></remarks>
      public static string SelectRequiredValue(string xml, string xpath)
      {
         return SelectRequiredValue(CreateDocument(xml), xpath);
      }
        /// <summary>
        /// Selects the required value.   
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="xpath">The xpath.</param>
        /// <returns></returns>
        /// <remarks></remarks>
      public static string SelectRequiredValue(XmlDocument document, string xpath)
      {
         XmlNode node = document.SelectSingleNode(xpath);
         if (node == null || node.InnerXml == null || (node.InnerXml != null && node.InnerXml.Length == 0))
         {
            throw new CruiseControlException("Document missing required value at xpath: " + xpath);
         }
         return node.InnerText;
      }
}

Related Tutorials