Showing posts with label xpath. Show all posts
Showing posts with label xpath. Show all posts

Wednesday, 17 December 2008

XPath in C#

string url = "http://www.example.com/somedata.xml";

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream());

string xmlstr = sr.ReadToEnd();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstr);

XmlNode root = doc.DocumentElement;

// Get a single node
XmlNode tnode = root.SelectSingleNode("/FEED/SOURCE");

// Get all nodes that match a certain XPath expression
XmlNodeList alltitles = root.SelectNodes("/BOOK/TITLE");

There is a useful table of XPath syntax on this page.