//---------------------------------------------------------------------
// 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
{
///
/// A utility class which applies XPath expressions to xml files and returns the value
/// in a variety of data types
///
public class XPathValidator
{
///
/// basic constructor
///
public XPathValidator()
{
//
// TODO: Add constructor logic here
//
}
///
/// Evaluates the XPath expression and returns a string value
///
/// full path of the xml file to parse
/// XPath expression to apply
/// string
public string GetStringValue(string InputXmlFile,string XPathString)
{
string retval ;
object obj = MakeXPathExpression(InputXmlFile,XPathString);
retval = (string)obj;
return(retval);
}
///
/// Evaluates the XPath expression and returns a integer value
///
/// full path of the xml file to parse
/// XPath expression to apply
/// int
public int GetIntegerValue(string InputXmlFile,string XPathString)
{
int retval ;
object obj = MakeXPathExpression(InputXmlFile,XPathString);
retval = System.Convert.ToInt32(obj);
return(retval);
}
///
/// Evaluates the XPath expression and returns a bool value
///
/// full path of the xml file to parse
/// XPath expression to apply
/// bool
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));
}
}
}