本文整理汇总了C#中HtmlAgilityPack.HtmlDocument.CreateNavigator方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument.CreateNavigator方法的具体用法?C# HtmlDocument.CreateNavigator怎么用?C# HtmlDocument.CreateNavigator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HtmlAgilityPack.HtmlDocument
的用法示例。
在下文中一共展示了HtmlDocument.CreateNavigator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Login
public void Login()
{
Dictionary<string, string> parameters;
// show login dialog
using (InsiderLoginDialog d = new InsiderLoginDialog())
{
if (d.ShowDialog() != System.Windows.Forms.DialogResult.OK)
throw new ApplicationException("User refused Insider login");
parameters = new Dictionary<string, string>();
parameters["email"] = d.Email;
parameters["password"] = d.Password;
parameters["InsiderSignin"] = "Sign In";
}
// perform login page GET to get event validation
using (HttpWebResponse response = HttpHelper.UrlGet(URL_LOGIN, m_cookies))
{
// extract the validation parameter from the response stream
using (Stream s = response.GetResponseStream())
{
HtmlDocument doc;
XPathNavigator nav_view, nav_event;
const string NAME_VIEW = "__VIEWSTATE";
const string NAME_EVENT = "__EVENTVALIDATION";
// read in the login page HTML
doc = new HtmlDocument();
doc.Load(s);
// select the elements we want to read
nav_view = doc.CreateNavigator().SelectSingleNode("//input[@name = '" + NAME_VIEW + "']");
nav_event = doc.CreateNavigator().SelectSingleNode("//input[@name = '" + NAME_EVENT + "']");
// if there was no such node, we're screwed
if (nav_view == null || nav_event == null)
throw new ApplicationException("Can't find parameters in login page");
// set value
parameters[NAME_VIEW] = nav_view.SelectSingleNode("@value").Value;
parameters[NAME_EVENT] = nav_event.SelectSingleNode("@value").Value;
}
}
// perform login request and grab the resulting cookie(s)
using (HttpWebResponse response = HttpHelper.UrlPost(URL_LOGIN, parameters, false, m_cookies))
{
// pass for now, should probably do some validation later
}
}
示例2: ParseUserPage
private List<user> ParseUserPage(string html)
{
List<user> list = new List<user>();
HtmlDocument docNav = new HtmlDocument();
docNav.LoadHtml(html);
XPathNavigator nav = docNav.CreateNavigator();
XPathNodeIterator i = nav.Select("//table[contains(@class, 'achievements') and contains(@class, 'real')]/tbody/tr");
while (i.MoveNext())
{
user u = new user();
XPathNavigator node = i.Current.SelectSingleNode("./td[contains(@class, 'name')]");
if (node != null) u.account = node.Value.Trim();
node = i.Current.SelectSingleNode("./td[contains(@class, 'achievements')]/span/span[not(contains(@class, 'additional'))]");
if (node != null) int.TryParse(node.Value.Trim(), out u.ap);
node = i.Current.SelectSingleNode("./td[contains(@class, 'achievements')]/span/span[contains(@class, 'additional')]");
if (node != null) u.lastapgain = node.Value.Remove(0, node.Value.IndexOf("Since") + 5).Replace("/", "/").Trim();
node = i.Current.SelectSingleNode("./td[contains(@class, 'world')]");
if (node != null) u.world = node.Value.Trim();
list.Add(u);
}
return list;
}
示例3: NotificationView
public NotificationView(JToken token_)
{
token = token_;
InitializeComponent();
// Getting link
foreach (var link in token_["link"])
{
if ((string) link["type"] == "text/html")
videoUri = new Uri((string) link["href"], UriKind.RelativeOrAbsolute);
}
// Parsing content to get icon
string unescaped_content = HttpUtility.HtmlDecode((string) token["content"]["$t"]);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(unescaped_content);
var navigator = doc.CreateNavigator();
var node = navigator.SelectSingleNode("//td[1]//img/@src");
imageUri = new Uri(node.Value);
BitmapImage bitmapImage = new BitmapImage(imageUri);
/* Bitmap newImage = new Bitmap(64, 64);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, 64, 64));
}
this.image.Source = new BitmapImage(uri);*/
this.image.Source = bitmapImage;
this.textBlockDescription.Text = ((string)token["author"][0]["name"]["$t"]) + " uploaded a new video: " + ((string)token["title"]["$t"]);
}
示例4: Import
public void Import(TextReader textReader, TextWriter textWriter)
{
this.textWriter = textWriter;
var htmlDocument = new HtmlDocument();
htmlDocument.Load(textReader);
var navigator = htmlDocument.CreateNavigator();
navigator.MoveToRoot();
RecursiveWalkThroughXpath(navigator);
textWriter.Flush();
}
示例5: IncludeItem
public void IncludeItem()
{
var control = new XslFile();
control.Path = "xsl/dc2011/navigation.xslt";
Sitecore.Context.Item = Sitecore.Context.Database.GetItem("/sitecore/content/dc2011");
var output = control.RenderAsText();
var doc = new HtmlDocument();
doc.LoadHtml(output);
var nav = doc.CreateNavigator();
var link = nav.Select("/nav[@class='primary']/ul/li[a/@href='/en/About Us.aspx']/a");
Assert.AreEqual(1, link.Count);
}
示例6: Analyse
protected void Analyse()
{
HtmlDocument hd = new HtmlDocument();
hd.LoadHtml(this.HtmlContent);
XPathNavigator xn = hd.CreateNavigator();
while (xn.MoveToFollowing(XPathNodeType.Element))
{
if (xn.LocalName == "a")
{
XPathNavigator xn_attr = xn.Clone();
if (xn_attr.MoveToAttribute("href", xn.NamespaceURI))
{
//Debug.Print("GotLink:{0}", xn_attr.Value);
this.Resources.Add(new Resource(new Uri(this.Url, xn_attr.Value), this));
}
}
}
}
示例7: ExtractLinks
public IEnumerable<Uri> ExtractLinks(Uri documentUrl, TextReader reader, string mediaType)
{
if (mediaType != MediaTypeNames.Text.Html && mediaType != "application/xhtml+xml")
yield break;
var document = new HtmlDocument();
document.Load(reader);
var xpath = document.CreateNavigator();
var absoluteUriBase = documentUrl;
var @base = (string)xpath.Evaluate("string(/html/head/base/@href)");
if (@base.IsNotNullOrEmpty()) {
// TODO: log this
if (!Uri.TryCreate(@base, UriKind.Absolute, out absoluteUriBase))
absoluteUriBase = documentUrl;
}
var hrefs = xpath.Select("//a/@href").Cast<XPathNavigator>().Select(x => x.Value);
foreach (var href in hrefs) {
Uri uri;
if (!Uri.TryCreate(href, UriKind.RelativeOrAbsolute, out uri)) /* broken link */ {
// TODO: log this
continue;
}
if (!uri.IsAbsoluteUri)
uri = new Uri(absoluteUriBase, uri);
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps && uri.Scheme != Uri.UriSchemeFtp) {
// TODO: log this
continue;
}
yield return uri;
}
}
示例8: MonsterInitiativeTableEntry
public MonsterInitiativeTableEntry(string description, string html)
: base(description, html)
{
// attempt to wade through the statblock tag soup and pull out the stats we need
try
{
HtmlDocument doc;
XPathNavigator nav;
XPathNodeIterator iter;
// load document and select all the bold thingies, they mark beginnings of things
doc = new HtmlDocument();
doc.LoadHtml(html);
nav = doc.CreateNavigator();
iter = nav.Select("//div[@id = 'detail']/p/b");
// we have some chance of finding what we want here...
while (iter.MoveNext())
{
switch (iter.Current.Value.Trim().ToLowerInvariant())
{
case "initiative":
if (iter.Current.MoveToNext(XPathNodeType.Text))
this.InitiativeBonus = TolerantParse(iter.Current.Value);
break;
case "ac":
if (iter.Current.MoveToNext(XPathNodeType.Text))
this.AC = TolerantParse(iter.Current.Value);
break;
case "fortitude":
if (iter.Current.MoveToNext(XPathNodeType.Text))
this.Fortitude = TolerantParse(iter.Current.Value);
break;
case "reflex":
if (iter.Current.MoveToNext(XPathNodeType.Text))
this.Reflex = TolerantParse(iter.Current.Value);
break;
case "will":
if (iter.Current.MoveToNext(XPathNodeType.Text))
this.Will = TolerantParse(iter.Current.Value);
break;
}
}
}
catch (Exception ex)
{
// we really ought to use a logger and track this properly
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
示例9: Run
public override CommandResult Run()
{
if (string.IsNullOrEmpty(Input))
return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("input"));
if (string.IsNullOrEmpty(XPath))
return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("xpath"));
XPathNavigator nav = null;
if (HAPRequired)
{
var doc = new HtmlDocument();
try
{
doc.LoadHtml(Input);
}
catch (Exception ex)
{
return new CommandResult(CommandStatus.Success, "Failed to parse input: " + ex.Message);
}
nav = doc.CreateNavigator();
}
else
{
var doc = new XmlDocument();
try
{
doc.LoadXml(Input);
}
catch (Exception ex)
{
return new CommandResult(CommandStatus.Success, "Failed to parse input: " + ex.Message);
}
nav = doc.CreateNavigator();
}
if (nav == null)
return new CommandResult(CommandStatus.Failure, "Failed to create XPath navigator");
// Process XML and extract any namespaces it contains. Allows using the namespaces in queries
var namespaceManager = new XmlNamespaceManager(nav.NameTable);
var allNodes = nav.Select("//*");
while (allNodes.MoveNext())
{
var namespaces = allNodes.Current.GetNamespacesInScope(XmlNamespaceScope.Local);
foreach (var ns in namespaces)
{
namespaceManager.AddNamespace(ns.Key, ns.Value);
}
}
var nodes = nav.Select(XPath, namespaceManager);
var lines = new List<string>();
while (nodes.MoveNext())
{
if (ValueOutput)
lines.Add(nodes.Current.Value);
else
lines.Add(nodes.Current.OuterXml);
}
var buffer = new StringBuilder(Formatter.JoinLines(lines));
if (!NoStats)
{
Formatter.PrintLine(string.Empty, buffer);
buffer.Append(string.Format("Matched {0} {1}", nodes.Count, (nodes.Count == 1 ? "node" : "nodes")));
}
return new CommandResult(CommandStatus.Success, buffer.ToString());
}
示例10: usage
public void usage(HtmlDocument html)
{
Assert.True(html.CreateNavigator().Evaluate<bool>("1 = count(//title[text()='Example'])"));
}
示例11: ParseSubHTMLDoc
private void ParseSubHTMLDoc(String SubLinkAdress)
{
System.Net.WebClient WClient = new System.Net.WebClient();
Byte[] Raw = WClient.DownloadData(String.Format(EntryWebAddressTemplate, SubLinkAdress));
String WebContent = System.Text.Encoding.UTF8.GetString(Raw);
HtmlDocument HtmlDoc = new HtmlDocument();
TextReader Reader = new StringReader(SkipKnownProblemStrings(WebContent));
HtmlDoc.Load(Reader);
HtmlDoc.OptionAddDebuggingAttributes = true;
XPathNavigator Nav = HtmlDoc.CreateNavigator();
XPathNodeIterator contentHeadPaneOpen = Nav.Select("//div[@class=\'page-header\']/h2[@itemprop=\'name\']");
contentHeadPaneOpen.MoveNext();
String HeadLine = contentHeadPaneOpen.Current.Value.Trim();
if (String.IsNullOrEmpty(HeadLine))
return;
AlpenVereinPlainTextDataEntry ActuallEntry = new AlpenVereinPlainTextDataEntry() { Parent = this };
AlpenVereinPlainTextDataEntries.Add(ActuallEntry);
ActuallEntry.EntryHeadLine.Add(HeadLine);
XPathNodeIterator ContentContentPaneOpen = Nav.Select("//div[@itemprop=\'articleBody\']");
ContentContentPaneOpen.MoveNext();
String Value = Basics.ConvertEscapedHTMLToString(ContentContentPaneOpen
.Current.Value.Replace("____NewLine____", "^")).Trim();
List<String> LinesToAdd = new List<string>();
foreach (String Line in Value.Split('^'))
if ((!String.IsNullOrEmpty(Line))
&& (Line.Trim() != ""))
LinesToAdd.Add(Line);
ActuallEntry.EntryContent.AddRange(LinesToAdd);
}
示例12: ParseHtmlDoc
private int ParseHtmlDoc (HtmlDocument HtmlDoc)
{
int NumberOfEntries = 0;
XPathNavigator Nav = HtmlDoc.CreateNavigator ();
XPathNodeIterator ContentPaneOpen = Nav.Select ("//div[@class=\'items-leading clearfix\']/div");
XPathNavigator HeadlineNavigator = ContentPaneOpen.Current;
XPathNodeIterator HeadlineIterator = HeadlineNavigator.Select("div[@itemprop=\'blogPost\']");
// Console.WriteLine ("---------------------------------------------------------------");
while (ContentPaneOpen.MoveNext ())
{
NumberOfEntries++;
XPathNodeIterator InnerContentIterator = ContentPaneOpen.Current.Select("div/h2[@itemprop=\'name\']/a");
InnerContentIterator.MoveNext();
ParseSubHTMLDoc(InnerContentIterator.Current.GetAttribute("href", ""));
//ActuallEntry.EntryContent.Add(InnerContentIterator.Current.Value.Trim());
//continue;
//ContentPaneOpen.MoveNext ();
//XPathNodeIterator ContentIterator = ContentPaneOpen.Current.Select("tr/td[@valign=\'top\']");
//while (ContentIterator.MoveNext())
// {
// int ContentCounter = 0;
// String Value = Basics.ConvertEscapedHTMLToString (ContentIterator.Current.Value.Replace("____NewLine____", "^")).Trim ();
// List<String> LinesToAdd = new List<string> ();
// foreach (String Line in Value.Split('^'))
// if ((!String.IsNullOrEmpty (Line))
// && (Line.Trim() != ""))
// LinesToAdd.Add(Line);
// ActuallEntry.EntryContent.AddRange(LinesToAdd);
// }
////String ID = ActuallEntry.InformationenNameID;
}
return NumberOfEntries;
}
示例13: usage
public void usage(HtmlDocument html)
{
var expected = new FileInfo("example.html").ReadToEnd();
var actual = html.CreateNavigator().OuterXml;
Assert.Equal(expected, actual);
}
示例14: GetData
public override IEnumerable<object[]> GetData(MethodInfo methodUnderTest,
Type[] parameterTypes)
{
if (null == methodUnderTest)
{
throw new ArgumentNullException("methodUnderTest");
}
if (null == parameterTypes)
{
throw new ArgumentNullException("parameterTypes");
}
#if NET20
if (IEnumerableExtensionMethods.Count(Files) != parameterTypes.Length)
{
throw new InvalidOperationException(StringExtensionMethods.FormatWith(Resources.Attribute_CountsDiffer, IEnumerableExtensionMethods.Count(Files), parameterTypes.Length));
}
#else
if (Files.Count() != parameterTypes.Length)
{
throw new InvalidOperationException(Resources.Attribute_CountsDiffer.FormatWith(Files.Count(), parameterTypes.Length));
}
#endif
var list = new List<object>();
var index = -1;
foreach (var file in Files)
{
var info = new FileInfo(file);
index++;
if (parameterTypes[index] == typeof(HtmlDocument) || parameterTypes[index] == typeof(IXPathNavigable))
{
var html = new HtmlDocument();
html.Load(info.FullName);
list.Add(html);
continue;
}
if (parameterTypes[index] == typeof(XPathNavigator))
{
var html = new HtmlDocument();
html.Load(info.FullName);
list.Add(html.CreateNavigator());
continue;
}
if (parameterTypes[index] == typeof(DataSet))
{
var html = new HtmlDocument();
html.Load(info.FullName);
#if NET20
list.Add(HtmlDocumentExtensionMethods.TabularData(html));
#else
list.Add(html.TabularData());
#endif
continue;
}
throw new InvalidOperationException(Resources.HtmlAttribute_UnsupportedParameterType);
}
yield return list.ToArray();
}
示例15: scrapeSyllabusPlus
private void scrapeSyllabusPlus(String url, int weekNumber)
{
String webPage = getWebpage(url);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(webPage);
// get an XPath navigator so we can browse the document:
XPathNavigator nav = doc.CreateNavigator();
// the second table is the one we care about:
XPathNodeIterator it = nav.Select("/html/body/table[2]");
it.MoveNext();
XPathNavigator rootTbl = it.Current;
XPathNodeIterator rows = rootTbl.Select("tr");
bool firstRow = true;
List<String> times = new List<String>();
String day = "";
foreach (XPathNavigator row in rows)
{
XPathNodeIterator cols = row.Select("td");
int currentCell = 0;
bool firstCol = true;
foreach (XPathNavigator col in cols)
{
// first row contains times. It would be nice if the first row was tagged with a CSS ID, but there you go..
if (firstRow)
{
times.Add(col.ToString());
}
else
{
// if the current cell has CSS class "row-label-one" then skip it - it's the day of week header
// although we may want to keep this so we know the date? nah...
if (firstCol)
{
firstCol = false;
day = col.ToString();
++currentCell;
continue;
}
// if the current cell has CSS class "object-cell-border then this is an appointment that needs to be
// synced!
if (col.Select("table").Count > 0)
//if (col.GetAttribute("class", "") == "object-cell-border")
{
// this is an event we need to sync:
// start time is the current cell lication:
String startTime = times.ElementAt(currentCell);
// end time is the current cell location plus colspan attribute:
int colspan = Int32.Parse(col.GetAttribute("colspan", ""));
String endTime = times.ElementAt(currentCell + colspan);
// there are three embedded <table> elements.
// the first one has the generic subject, like "Bachelor of Information Technology".
String department = getStringFromXSLTPath(col, "table[1]/tr/td");
// the second has the specific subject and type, like "Web Fundamentals", "Lecture"
String subject = getStringFromXSLTPath(col, "table[2]/tr/td[1]");
String subjType = getStringFromXSLTPath(col, "table[2]/tr/td[2]");
// the third has the weeks and room info.
String room = getStringFromXSLTPath(col, "table[3]/tr/td[2]");
// work out the date we're on. We know the week we're in, and we can get the week day number easily enough...
DateTime startDT = getDateTimeFromDayAndWeek(day, weekNumber, startTime);
DateTime endDT = getDateTimeFromDayAndWeek(day, weekNumber, endTime);
createCalendarEvent(startDT, endDT, department, subject, subjType, room);
// finished processing, so add the current colspan to the current cell number:
currentCell += colspan;
}
else
{
++currentCell;
}
}
}
// completed at least one row:
firstRow = false;
}
}