2012-10-09 20 views
0

嗨我正在試驗如何使用正則表達式使用HTMLAgilityPack V(不知道哪個更昂貴)。我與HTMLAgilityPack問題ID我能夠提取所需的屬性,並用新的替換它;但是,我似乎不能更新原文。這是代碼;C#:找到HTML屬性並使用HTMLAgilityPack替換值

string input = @"<area shape=""rect"" coords=""0,0,82,126"" href=""one"" alt=""Sun""> <area shape=""rect"" coords=""0,0,82,126"" href=""two"" alt=""Sun"" > <area shape=""rect"" coords=""0,0,82,126"" href=""Three"" alt=""Sun"" >"; 

HtmlDocument document = new HtmlDocument(); 
document.LoadHtml(input); 

HtmlNodeCollection nodes = document.DocumentNode.SelectNodes("//area"); 

for (int i = 0; i < nodes.Count; i++) 
{ 
    HtmlNode node = nodes[i]; 
    var href = node.Attributes["href"].Value; 
    //Reassigning href value 
    node.Attributes["href"].Value="Re-Assign ["+i+"]"; 
} 

現在我想使這反映在原來的「輸入」變量。任何想法如何繼續?

感謝

回答

1

我也有如何使用敏捷性包試驗。試用:

String HTML = nodes.DocumentNode.WriteTo(); 

你需要在原文中寫更新。

0

你錯過了doc.Save(yourFileName);