https://github.com/mntone/xpath
Simplified xpath function generator.
https://github.com/mntone/xpath
Last synced: 5 months ago
JSON representation
Simplified xpath function generator.
- Host: GitHub
- URL: https://github.com/mntone/xpath
- Owner: mntone
- License: mit
- Created: 2015-12-07T03:17:57.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-07T05:22:00.000Z (over 9 years ago)
- Last Synced: 2025-01-23T08:53:12.089Z (5 months ago)
- Language: C#
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# XPath
[](https://ci.appveyor.com/project/mntone/XPath) [](https://www.nuget.org/packages/Mntone.XPath/) [](https://www.nuget.org/packages/Mntone.XPath/) [](https://github.com/mntone/XPath/blob/master/LICENSE.txt)
This is simplified xpath function generator for .NET.
## Requirement
- Each parser (e.g. HtmlAgilityPack)
## Usage
In this case, use HtmlAgilityPack together.
1. Install XPath.
2. Create class `XPathRetriveConfig.````csharp
internal sealed class HtmlAgilityXPathRetriveConfig : IXPathRetriveConfig
{
public static HtmlAgilityXPathRetriveConfig Instance { get { return _Instance ?? (_Instance = new HtmlAgilityXPathRetriveConfig()); } }
private static HtmlAgilityXPathRetriveConfig _Instance = null;private HtmlAgilityXPathRetriveConfig() { }
public Expression>> ChildrenGetter => node => node.ChildNodes;
public Expression> TagNameGetter => node => node.Name;
public Expression> TextGetter => node => node.InnerText;
public Expression> AttributeValueGetter => (node, name) => node.GetAttributeValue(name, string.Empty);
}
```3. Compile function and parse.
```csharp
public string GetFirstDivText(string html)
{
var func = XPathExpression.Compile("/html/body/div[0]/text()", HtmlAgilityXPathRetriveConfig.Instance);var doc = new HtmlDocument();
doc.Load(new StringReader(html));var result = func(new[] { doc.DocumentNode }).Single();
return result;
}
```## LICENSE
[MIT License](https://github.com/mntone/XPath/blob/master/LICENSE.txt)
## Author
- mntone
GitHub: https://github.com/mntone
Twitter: https://twitter.com/mntone (posted in Japanese; however, english is ok)