2011-12-29 82 views
8

我有一個HTML文檔,並使用XPath解析它。我想獲得元素輸入的值,但它不起作用。通過XPath和HtmlAgilityPack獲取屬性的值

我的HTML:

<tbody> 
    <tr> 
    <td> 
     <input type="text" name="item" value="10743" readonly="readonly" size="10"/> 
    </td> 
    </tr> 
</tbody> 

我的代碼:

using HtmlAgilityPack; 

HtmlAgilityPack.HtmlDocument doc; 
HtmlWeb hw = new HtmlWeb(); 
HtmlNodeCollection node = doc.DocumentNode.SelectNodes("//input/@value"); 
string s=node[0].InnerText; 

所以我想要得到的值: 「10743」(我不介意讓與另一個標籤回答。)

+0

你試過'node [0] .Value'嗎? – Oded 2011-12-29 10:55:13

+0

不,因爲我想通過'node [0] .InnerText' – 2011-12-29 10:59:05

+1

獲取值但是一個屬性沒有'InnerText'。 – Oded 2011-12-29 12:16:31

回答

6

更新2:下面是一個代碼示例如何使用Html Agility Pack獲取屬性值:

http://htmlagilitypack.codeplex.com/wikipage?title=Examples

HtmlDocument doc = new HtmlDocument(); 
doc.Load("file.htm"); 
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"]) 
{ 
    HtmlAttribute att = link.Attributes["href"]; 
    att.Value = FixLink(att); 
} 
doc.Save("file.htm"); 

你顯然需要將此代碼適應您的需求 - 比如你將不會修改屬性,而只是用att.Value


更新:您也可以看看這個問題:

Selecting attribute values with html Agility Pack


你的問題很可能是默認命名空間問題 - 搜索「的XPath默認命名空間C#「,你會發現很多好的解決方案(提示:使用SelectNodes(),它有一個XmlNamespaceManager變元)。

下面的代碼顯示了一個獲取對文檔中的一個屬性 「沒有命名空間」:

using System; 
using System.IO; 
using System.Xml; 

public class Sample 
{ 

    public static void Main() 
    { 

     XmlDocument doc = new XmlDocument(); 
     doc.LoadXml("<input value='novel' ISBN='1-861001-57-5'>" + 
        "<title>Pride And Prejudice</title>" + 
        "</input>"); 

     XmlNode root = doc.DocumentElement; 

     XmlNode value = doc.SelectNodes("//input/@value")[0]; 

     Console.WriteLine("Inner text: " + value.InnerText); 
     Console.WriteLine("InnerXml: " + value.InnerXml); 
     Console.WriteLine("OuterXml: " + value.OuterXml); 
     Console.WriteLine("Value: " + value.Value); 

    } 
} 

運行這個程序的結果是

Inner text: novel 
InnerXml: novel 
OuterXml: value="novel" 
Value: novel 

現在,對於處於默認命名空間的文檔:

using System; 
using System.IO; 
using System.Xml; 

public class Sample 
{ 

    public static void Main() 
    { 

     XmlDocument doc = new XmlDocument(); 
     doc.LoadXml("<input xmlns='some:Namespace' value='novel' ISBN='1-861001-57-5'>" + 
        "<title>Pride And Prejudice</title>" + 
        "</input>"); 

     XmlNode root = doc.DocumentElement; 

     XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); 
     nsmgr.AddNamespace("x", "some:Namespace"); 

     XmlNode value = doc.SelectNodes("//x:input/@value", nsmgr)[0]; 

     Console.WriteLine("Inner text: " + value.InnerText); 
     Console.WriteLine("InnerXml: " + value.InnerXml); 
     Console.WriteLine("OuterXml: " + value.OuterXml); 
     Console.WriteLine("Value: " + value.Value); 

    } 
} 

運行這個程序,再想要的結果生產:

Inner text: novel 
InnerXml: novel 
OuterXml: value="novel" 
Value: novel 
+0

謝謝,但這不是問題,我的文檔是Html,另一個XPath doe很好,除此之外 - 因爲這個XPath不適合我的意圖。我需要找到另一個XPath,但我不知道。 – 2011-12-29 14:49:06

+0

我不是很清楚嗎?無論如何,我添加**所有**我的代碼,並寫了我想要的:字符串:「** 10743 **」(節點輸入的值) – 2011-12-29 16:18:05

+0

@Chanipoz:看看我的第二次更新 - 一個代碼示例顯示如何使用Html Agility Pack獲取屬性的價值 - 您可以輕鬆地適應您的需求。 – 2011-12-29 16:30:30

14

你可以得到它在.Attributes集合:

var doc = new HtmlAgilityPack.HtmlDocument(); 
doc.Load("file.html"); 
var node = doc.DocumentNode.SelectNodes("//input") [0]; 
var val = node.Attributes["value"].Value; //10743 
5

您也可以直接搶屬性,如果你使用HtmlNavigator

//Load document from some html string 
HtmlDocument hdoc = new HtmlDocument(); 
hdoc.LoadHtml(htmlContent); 

//load navigator for current document 
HtmlNavigator navigator = (HtmlNodeNavigator)hdoc.CreateNavigator(); 

//Get value with given xpath 
string xpath = "//input/@value"; 
string val = navigator.SelectSingleNode(xpath).Value;