Skip to content

Instantly share code, notes, and snippets.

@lmatt-bit
Created March 7, 2012 16:20
Show Gist options
  • Select an option

  • Save lmatt-bit/1994164 to your computer and use it in GitHub Desktop.

Select an option

Save lmatt-bit/1994164 to your computer and use it in GitHub Desktop.
C# XPath search
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
string xmlText = "<root>" +
"<a href=\"/PatternFramework/Pages/LogOut.aspx?np=/sites/novatestsite/Home.aspx\" slick-uniqueid=\"92\">Sign out</a>" +
"</root>";
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
String strExpression;
docNav = new XPathDocument(new StringReader(xmlText));
nav = docNav.CreateNavigator();
strExpression = "//a[contains(lower-case(@href), \"logout.aspx\")]";//xpath 2.0, c# not support
strExpression = "//a[contains(translate(@href, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'logout.aspx')]";//xpath 1.0
NodeIter = nav.Select(strExpression);
while(NodeIter.MoveNext())
{
Console.WriteLine("href={0}", NodeIter.Current.GetAttribute("href", ""));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment