Selects the XML value via xpath - CSharp System.Xml

CSharp examples for System.Xml:XPath

Description

Selects the XML value via xpath

Demo Code


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

public class Main{
        /// <summary>
        /// Selects the value.   
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="xpath">The xpath.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        /// <remarks></remarks>
      public static string SelectValue(XmlDocument document, string xpath, string defaultValue)
      {
         XmlNode node = document.SelectSingleNode(xpath);
         return SelectValue(node, xpath, defaultValue);
      }
        /// <summary>
        /// Selects the value.   
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="xpath">The xpath.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        /// <remarks></remarks>
      public static string SelectValue(XmlNode node, string xpath, string defaultValue)
      {
         if (node == null || node.InnerXml == null || (node.InnerXml != null && node.InnerXml.Length == 0))
         {
            return defaultValue;
         }
         node = node.SelectSingleNode(xpath);
         if (node == null)
         {
            throw new ArgumentException("No node found at: " + xpath);
         }
         return node.InnerText;
      }
}

Related Tutorials