Get XML Node Value - CSharp System.Xml

CSharp examples for System.Xml:XML Node

Description

Get XML Node Value

Demo Code

//              Copyright (c) 2006-2017 All Rights reserved                   *
using System.Xml.XPath;
using System.Xml;
using System.IO;//from w w  w  .j av  a  2 s. c  o m
using System.Globalization;
using System;

public class Main{
        public static string GetNodeValue(XmlNode element, string XPath, string defaultValue)
      {
         try
         {
            XmlNode node = null;
            node = element.SelectSingleNode(XPath);
            if (node == null)
               return defaultValue;
            else
               return node.InnerText;
         }
         catch (Exception ex)
         {
            throw;
         }
      }
        #endregion

      #region GetNodeValue

      public static string GetNodeValue(XmlDocument document, string XPath, string defaultValue)
      {
         try
         {
            return GetNodeValue(document.DocumentElement, XPath, defaultValue);
         }
         catch (Exception ex)
         {
            throw;
         }
      }
}

Related Tutorials