XPath i .net
Jeg har et addon til Firefox som hedder Xpather. Her kan jeg finde mig frem til "United States" i toppen på http://www.microsoft.com/en/us/default.aspx .Dens xpath er: /html/body[@class='ltr']/form[@id='aspnetForm']/div[@class='h15-bottom' and position()=2]/div[@class='h15-header' and position()=1]/div[@class='h15-header-inner' and @id='hpToolbar']/strong[@class='h15-country']
Hvordan bruger jeg det i min kode? Jeg har prøvet:
SgmlReader r = new SgmlReader();
r.Href = "http://www.microsoft.com/en/us/default.aspx";
XmlDocument doc = new XmlDocument();
doc.Load(r);
XmlNode firstNavItemNode = doc.SelectSingleNode("/html/body[@class='ltr']/form[@id='aspnetForm']/div[@class='h15-bottom' and position()=2]/div[@class='h15-header' and position()=1]/div[@class='h15-header-inner' and @id='hpToolbar']/strong[@class='h15-country']");
Debug.Assert(firstNavItemNode.InnerText == "United States");
Jeg bruger SGMLReader fra http://wiki.developer.mindtouch.com/SgmlReader