如何阅读子元素“strong”跨度? C# [英] How do I read the child element "strong" of a span? C#
问题描述
大家好,
我正在尝试阅读跨度的子元素强。我希望然后把它转换为字符串。
所以在这种情况下我试图读取强值,即4号。 br />
有没有人有任何想法我怎么能做到这一点。
这是一份HTML的副本网页供参考。
Imgur:互联网上最棒的图片 [ ^ ]
提前谢谢
我的尝试:
我已经尝试过在各种论坛上观看,包括这个,没有运气。
我目前为止代码
Hi Guys,
I'm trying to read the child element "Strong" of a span. I'm looking to then convert it to a string.
so in this case I'm trying to read the "strong" value which is the number 4.
Does anyone have any ideas how i can achieve this please.
Here is a copy of the HTML web page for reference.
Imgur: The most awesome images on the Internet[^]
Thank you in advance
What I have tried:
I have already tried looking on various forums including this one and no luck.
Code i have so far
HtmlElementCollection allelements = _webBrowser.Document.All;
foreach (HtmlElement webpageelement in allelements)
{
if ((webpageelement.GetAttribute("span") == "4"))
{
MessageBox.Show("Show");
}
}
自从学到目前为止所说的这个帖子我有这个。
since learning from what is said so far in this thread i have this.
HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("div");
foreach (HtmlElement curElement in theElementCollection)
{
if (curElement.GetAttribute("className").ToString() == "ng-scope")
{
HtmlElementCollection childDivs = curElement.Children.GetElementsByName("strong");
foreach (HtmlElement childElement in childDivs)
{
MessageBox.Show(childElement.InnerText);
}
}
}
推荐答案
HtmlElementCollection theElementCollection = default(HtmlElementCollection);
// you need a list of the "span" elements, not "div"
theElementCollection = _webBrowser.Document.GetElementsByTagName("span");
foreach (HtmlElement curElement in theElementCollection)
{
if (curElement.GetAttribute("className").ToString() == "ng-scope")
{
// there isn't a GetElementsByTagName (GetElementsByName is something else) on the child element so we'll
// iterate through all children....
HtmlElementCollection childDivs = curElement.Children;
foreach (HtmlElement childElement in childDivs)
{
// ..and check the TagName for "strong"
if (string.Compare(childElement.TagName, "strong", true) == 0)
{
MessageBox.Show(childElement.InnerText);
}
}
}
}
除了灵魂1之外 F-ES Sitecore [ ^ ],这组文章可能对您有用:
< a href =https://www.codeproject.com/Tips/804660/How-to-Parse-HTML-using-Csharp>如何使用C#解析HTML [ ^ ]
< a href =https://htmlagilitypack.codeplex.com/> Html Agility Pack - 主页 [ ^ ]
使用HtmlAgilityPack解析ASP.NET中的HTML [ ^ ]
GitHub - AngleSharp / AngleSharp:最终尖括号解析器库解析HTML5,MathML,SVG和CSS以构建基于官方W3C规范。 [ ^ ]
In addition to soultion 1 by F-ES Sitecore[^], this set of articles might be hepful to you:
How to Parse HTML using C# [^]
Html Agility Pack - Home[^]
Using the HtmlAgilityPack to parse HTML in ASP.NET[^]
GitHub - AngleSharp/AngleSharp: The ultimate angle brackets parser library parsing HTML5, MathML, SVG and CSS to construct a DOM based on the official W3C specifications.[^]
这篇关于如何阅读子元素“strong”跨度? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!