Parsing XPath节点选择

Parsing XPath节点选择,parsing,windows-phone-8,xpath,html-agility-pack,nodes,Parsing,Windows Phone 8,Xpath,Html Agility Pack,Nodes,我正在使用HtmlAgilityPack解析Windows Phone 8应用程序的数据。我管理了四个节点,但在最后一个节点上遇到了困难 Game newGame = new Game(); newGame.Title = div.SelectSingleNode(".//section//h3").InnerText.Trim(); newGame.Cover = div.SelectSingleNode(".//section//img").Attributes["src"].Value;

我正在使用HtmlAgilityPack解析Windows Phone 8应用程序的数据。我管理了四个节点,但在最后一个节点上遇到了困难

Game newGame = new Game();
newGame.Title = div.SelectSingleNode(".//section//h3").InnerText.Trim();
newGame.Cover = div.SelectSingleNode(".//section//img").Attributes["src"].Value;
newGame.Summary = div.SelectSingleNode(".//section//p").InnerText.Trim();
newGame.StoreLink = div.SelectSingleNode(".//img[@class= 'Store']").Attributes["src"].Value;
newGame.Logo = div.SelectSingleNode(".//div[@class= 'text-col'").FirstChild.Attributes["src"].Value;
最后一段代码就是我遇到问题的代码。网站上的HTML看起来像这样简化了我需要的数据

<div id= "ContentBlockList" class="tier ">
   <section>
      <div class="left-side"><img src="newGame.Cover"></div>
      <div class="text-col">
         <img src="newGame.Logo http://url.png" />
         <h3>newGame.Title</h3>
         <p>new.Game.Summary</p>
         <a href="https://link to online store"><img src="newGame.StoreLink" class="Store" /></a>
      </div>
</div>
</section>
但是,我不知道如何让第二个imgsrc检索商店徽标。有什么想法吗

newGame.Cover = div.SelectSingleNode(".//img[2]").Attributes["src"].Value;
您没有发布全部内容,但这应该可以解决问题。

您可以这样尝试:

newGame.Cover = div.SelectSingleNode("(.//img)[2]")
                   .GetAttributeValue("src", "");
GetAttributeValue优于Attributes[…]


旁注:您的HTML标记无效,例如,某些元素未关闭。这可能会引起混乱。

谢谢您的回复。但是,我出现了以下错误:System.Xml.XPath.XPathException类型的异常发生在System.Xml.XPath.DLL中,但未在用户代码中处理其他信息:“.img[2]”具有无效标记。您可以像我一样使用格式化HTML片段。现在我们可以清楚地看到,节开始和结束标记不在InlineEWGAME.Cover=div.SelectSingleNode//img[2].Attributes[src].Value;
newGame.Cover = div.SelectSingleNode("(.//img)[2]")
                   .GetAttributeValue("src", "");