如何在C#中的字符串中提取href标记? [英] How to extract href tag from a string in C#?

查看:236
本文介绍了如何在C#中的字符串中提取href标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回以下格式的字符串一个C#功能:

I have a C# function that returns a string in the following format:

string tableTag = "<th><a href="Boot_53.html">135 Boot</a></th>"



我想要得到的HREF链接,存储到另一个字符串称为链接:

I want to get the href link and store into another string called link:

string link = "Boot_53.html"

我怎么能做到这一点在C#?

How can I do that in C#?

推荐答案

如果您知道的HTML实际上是一个XHTML(一个HTML中符合XML standarts [或多或少]),您可以分析只是专用于XML工具(这是一般比那些HTML)简单。

If you know that the html is actually a xhtml (an html which conforms to the xml standarts [more or less]) you can parse is simply with tools dedicated to xml (which are generally simpler than those for html).

var hrefLink = XElement.Parse("<th><a href=\"Boot_53.html\">135 Boot</a></th>")
                       .Descendants("a")
                       .Select(x => x.Attribute("href").Value)
                       .FirstOrDefault();

这篇关于如何在C#中的字符串中提取href标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆