简体   繁体   English

获取C#中特定HTML标签(span标签)的值

[英]Get value of specific HTML tag(span tag) in C#

I am developing a GOOGLE TRANSLATE software for Windows Phonw 8. I want to get the "value of ALL SPAN TAGS" inside a span tag of specific class="result_box" in C#. 我正在为Windows Phonw 8开发GOOGLE TRANSLATE软件。我想在C#中的特定类=“ result_box”的span标签内获取“ ALL SPAN TAGS的值”。

<html>
.
.
<span id="result_box" class="short_text" lang="pt">
        <span class="hps">
            Olá
        </span>
        <span class="">
            .
        </span>
        <span class="hps">
            oi
        </span>
    </span>
.
.
</html>

I tried this but it is not working 我尝试了这个,但是没有用

html = e.Result;
var r = new Regex(@"(?i)<span[^>]*?>\s*", RegexOptions.IgnoreCase);
string capture = r.Match(html).Groups[1].Value;
MessageBox.Show(capture);

Suggest me REGEX. 向我建议REGEX。 If possible please give me full function that returns me the text. 如果可能的话,请给我完整的功能,让我返回文字。

what about this? 那这个呢?

        Regex r = new Regex(@"<span[^>].*?>([^<]*)<\/span>", RegexOptions.IgnoreCase);

        foreach (Match matchedSpan in r.Matches(html))
        {
            string capture = matchedSpan.Groups[1].Value;
            MessageBox.Show(capture);
        }

Ok since @mason didn't like the previous answer, here's goes another aproach: 好的,因为@mason不喜欢先前的答案,所以这是另一个方法:

        XmlDocument htmlXML=new XmlDocument();
        htmlXML.LoadXml(html);
        foreach (XmlNode spanElement in htmlXML.SelectNodes("//span[@class='short_text']/span") ) {
            MessageBox.Show(spanElement.InnerText);

        }

remember to add 记得添加

using System.Xml;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM