[英]Parsing HTML string in WP7
我需要解析从服务器收到的HTML字符串。
<html>
<head/>
<body style="margin: 0;padding: 0">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa
/wa/viewSoftware?id=319737742&mt=8&uo=6" style="margin: 0;padding: 0"><img
src="https://s3.amazonaws.com/sportschatter/postcard.jpg" style="margin: 0;padding:
0"/></a>
</body>
</html>
这是我从服务器获得的响应。 我需要检索img
URL https://s3.amazonaws.com/sportschatter/postcard.jpg
以及href
部分。 我有适用于WP7的HTML Agility包,但是我不知道如何编写查询来获取此信息。 我尝试过这样的事情:
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(htmlString);
var value = document.DocumentNode.Descendants("img src").
Select(
x =>
x.InnerText);
这没有任何价值。 我也尝试过Regex
:
string parseString = htmlstring;
Regex expression = new Regex(@".*img src=(\d+).*$");
Match match = expression.Match(parseString);
MessageBox.Show(match.Groups[1].Value);
但这也不起作用。 请让我知道我在做什么错。
您显然误解了使用LINQ2XML语法的含义(没有XPath,因为Windows Phone不支持XPath)
您需要执行以下操作:
var image = document.DocumentNode.Descendants("img").First()
var source = image.GetAttribute("src", "").Value;
使用HtmlAgilityPack-不要使用正则表达式。
Descendants
的“查询字符串”是XPath,而不是类似CSS的选择器。
这是一个示例: http : //htmlagilitypack.codeplex.com/wikipage?title=Examples这是有关XPath的一些信息: http : //msdn.microsoft.com/zh-cn/library/ms256086.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.