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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » XML » XPathScreenshots 
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.CompileXPathString);

      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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.