HTML:
<strong>Capture Date/Time:</strong> August 1, 2012 1:05:00 PM EST<br>
<strong>Instructor:</strong> Ash<br>
<strong>Instructor Email:</strong> email@email.com<br>
<strong>Course ID:</strong> Course321<br>
Comment pourrais-je aller pour obtenir le texte à droite de chaque nœud fort?
Par exemple, pour obtenir l'identifiant du cours, je me suis retrouvé avec une chaîne de caractères "Course321".
Code:
private string getCourseID()
{
foreach (HtmlAgilityPack.HtmlNode strong in htmlDoc.DocumentNode.SelectNodes("//strong"))
{
string innerText = strong.InnerText;
if (innerText.Contains("Course ID"))
{
//select the outer text
//return outertext;
}
}
}
Code actuel:
private string getCourseID()
{
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
string value = "Error";
foreach (HtmlAgilityPack.HtmlNode strong in htmlDoc.DocumentNode.SelectNodes("//strong"))
{
string innerText = strong.InnerText;
if (innerText.Contains("Course ID"))
{
HtmlAgilityPack.HtmlNode sibling = strong.SelectSingleNode("following-sibling::text()");
value = sibling.InnerText.Trim();
MessageBox.Show(value);
}
}
return value;
}
À l'aide de l'axe suivant-frère :: * XPath:
HtmlNode sibling = strong.SelectSingleNode("following-sibling::text()");
Console.WriteLine("Course ID = " + sibling.InnerText.Trim());
Pour ceux d'entre vous qui partagent mon XPathofobia, voici ce que vous feriez pour que les frères et sœurs affichent des tags forts:
new HtmlDocument().LoadHtml("blah blah blah").DocumentNode.DescendantsAndSelf().Where (dn => dn.Name == "strong").Select (dn => dn.NextSibling.InnerText)