Gets the text value from the element located by the given XPath. : 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » XML » XPathScreenshots 
Gets the text value from the element located by the given XPath.
       

/*
 * RegExpress
 
 * Copyright (c) 2010, Daniel McGaughran
 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 
 *   http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 
 */


using System;
using System.Xml;


namespace RegExpressModel.Utility
{
  static class XmlUtility
  {



    /// <summary>
    /// Gets the text value from the element located by the given XPath.
    /// </summary>
    /// <param name="sourceNode">The source element.</param>
    /// <param name="xPath">The XPath of the parent element.</param>
    /// <returns>The (first) string value if found. If the source element or XPath is invalid, null will
    /// be returned.</returns>
    internal static string GetTextValue(this XmlNode sourceNode, string xPath)
    {
      if (sourceNode == null || String.IsNullOrEmpty(xPath))
        return null;

      XmlNode targetParent = sourceNode.SelectSingleNode(xPath);
      string value = "";

      if (targetParent != null)
      {
        XmlNodeList children = targetParent.ChildNodes;

        foreach(XmlNode childNode in children)
        {
          if (childNode is XmlText || childNode is XmlCDataSection)
          {
            value = childNode.Value;
            break;
          }
        }
      }

      return value;
    }

  }
}

   
    
    
    
    
    
    
  
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.Using XPath to get string value, integer value and boolean value
10.Extensions.XPathSelectElement selects an XElement using a XPath expression
11.Extensions.XPathSelectElements Selects a collection of elements using an XPath expression.
12.Returns the string result from evaluating an xpath expression against the given document and context.
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.