2013-05-05 148 views
0

我正在一個項目中,我需要分析很多html文件。我需要從一個<div class="story-body">在HtmlAgilityPack中獲取其他元素的特定元素在C#

每個<p>到目前爲止,我有這個代碼,它做我想做的,但我想知道如何使用xpath表達式來做到這一點。我試過這個:

textBody.SelectNodes ("What to put here? I tried //p but it gives every p in document not inside the one div") 

但是沒有成功。有任何想法嗎?

public void Parse(){ 
    HtmlNode title = doc.DocumentNode.SelectSingleNode ("//h1[(@class='story-header')]"); 
    HtmlNode textBody = doc.DocumentNode.SelectSingleNode ("//div[(@class='story-body')]"); 

    XmlText textT; 
    XmlText textS; 

    string story = ""; 

    if(title != null){ 
    textT = xmlDoc.CreateTextNode(title.InnerText); 
    titleElement.AppendChild(textT); 
    Console.WriteLine(title.InnerText); 
    } 

    foreach (HtmlNode node in textBody.ChildNodes) { 
     if(node.Name == "p" || (node.Name == "span" && node.GetAttributeValue("class", "class") == "cross-head")){ 
     story += node.InnerText + "\n\n"; 
     Console.WriteLine(node.InnerText); 
     } 
    } 

    textS = xmlDoc.CreateTextNode (story); 

    storyElement.AppendChild (textS); 

    try 
    { 
     xmlDoc.Save("test.xml");    
    } 
    catch (Exception e) 
    { 
     Console.WriteLine(e.Message); 
    } 
} 

回答

0

這是一個相當簡單的事情,你只需要一個.添加到字符串像.//p,這樣你只能得到當前節點的子節點。

另一種方法是隻是調用的SelectNodes這樣的:

doc.DocumentNode.SelectNodes("//div[(@class='story-body')]/p"); 
+0

謝謝你,你是對的很簡單。 但是,我結束了我的原始方法,因爲我必須檢查更多的東西,我不認爲它可以用xpath實現 – Jan 2013-05-06 16:48:27