Using XPath to get string value, integer value and boolean value : XPath « XML « C# / C Sharp






Using XPath to get string value, integer value and boolean value

     
//---------------------------------------------------------------------
// File: XPathValidator.cs
// 
// Summary: 
//
// Copyright (c) http://bizunitextensions.codeplex.com. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Xml.XPath;
using System.Xml;

namespace BizUnit.Extensions.Utilities
{
  /// <summary>
  /// A utility class which applies XPath expressions to xml files and returns the value
  /// in a variety of data types
  /// </summary>
  public class XPathValidator
  {
    /// <summary>
    /// basic constructor
    /// </summary>
    public XPathValidator()
    {
      //
      // TODO: Add constructor logic here
      //
    }
    /// <summary>
    /// Evaluates the XPath expression and returns a string value
    /// </summary>
    /// <param name="InputXmlFile">full path of the xml file to parse</param>
    /// <param name="XPathString">XPath expression to apply</param>
    /// <returns>string</returns>
    public string GetStringValue(string InputXmlFile,string XPathString)
    {
      string retval ;
      object obj = MakeXPathExpression(InputXmlFile,XPathString);
      retval = (string)obj;
      return(retval);
    }

    /// <summary>
    /// Evaluates the XPath expression and returns a integer value
    /// </summary>
    /// <param name="InputXmlFile">full path of the xml file to parse</param>
    /// <param name="XPathString">XPath expression to apply</param>
    /// <returns>int</returns>
    public int GetIntegerValue(string InputXmlFile,string XPathString)
    {
      int retval ;
      object obj = MakeXPathExpression(InputXmlFile,XPathString);
      retval = System.Convert.ToInt32(obj);
      return(retval);
    }

    /// <summary>
    /// Evaluates the XPath expression and returns a bool value
    /// </summary>
    /// <param name="InputXmlFile">full path of the xml file to parse</param>
    /// <param name="XPathString">XPath expression to apply</param>
    /// <returns>bool</returns>
    public bool GetBooleanValue(string InputXmlFile,string XPathString)
    {
      bool retval ;
      object obj = MakeXPathExpression(InputXmlFile,XPathString);
      retval = System.Convert.ToBoolean(obj);
      return(retval);
    }
    private object MakeXPathExpression(string inputXmlFile,string XPathString)
    {
      
      XmlDocument xDoc = new XmlDocument();
      xDoc.Load(inputXmlFile);
      XPathNavigator nav = xDoc.CreateNavigator();
      XPathExpression expr = nav.Compile( XPathString);

      return(nav.Evaluate(expr));
    }
  }
}

   
    
    
    
    
  








Related examples in the same category

1.XPathNavigator
2.XPathNodeIterator
3.Select by path
4.Find Elements with an XPath SearchFind Elements with an XPath Search
5.XPath Query Demo
6.Read XML node using the XML path
7.XmlQuery Example
8. XPath Expression Syntax
9.Extensions.XPathSelectElement selects an XElement using a XPath expression
10.Extensions.XPathSelectElements Selects a collection of elements using an XPath expression.
11.Returns the string result from evaluating an xpath expression against the given document and context.
12.Gets the text value from the element located by the given XPath.
13.Returns the inner text of the single node selected from the specified xpath if found; otherwise, null.
14.Get an array of nodes matching an XPath expression
15.Schema Reference Util