2016-09-21 64 views
1

是否可以使用C#HTML Agility Pack將變量插入選定節點?使用Html Agility Pack C將變量注入html輸入標記值#

我已經創建了我的HTML表單,加載它,並選擇我想要的輸入節點,現在我想在價值領域注入SAML響應

這裏是一個位碼的我有,首先在HTML文檔:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title></title> 
</head> 
<body runat="server" id="bodySSO"> 
    <form id="frmSSO" runat="server" enableviewstate="False"> 
     <div style="display:none" > 
      <input id="SAMLResponse" name="SAMLResponse" type="text" runat="server" enableviewstate="False" value=""/> 
      <input id="Query" name="Query" type="text" runat="server" enableviewstate="False" value=""/> 
     </div> 
    </form> 
</body> 
</html> 

,這裏是它加載的HTML文件,並選擇我想要的節點的功能:

public static string GetHTMLForm(SamlAssertion samlAssertion) 
{ 
    HtmlAgilityPack.HtmlDocument HTMLSamlDocument = new HtmlAgilityPack.HtmlDocument(); 
    HTMLSamlDocument.Load(@"C:\HTMLSamlForm.html"); 
    HtmlNode node = HTMLSamlDocument.DocumentNode.SelectNodes("//input[@id='SAMLResponse']").First(); 

    //Code that will allow me to inject into the value field my SAML Response 
} 

編輯:

好了,所以我已經實現了SAML響應數據包注入HTML的輸入標籤的這個「價值」字段:

HtmlAgilityPack.HtmlDocument HtmlDoc = new HtmlAgilityPack.HtmlDocument(); 
String SamlInjectedPath = "C:\\SamlInjected.txt"; 
HtmlDoc.Load(@"C:\HTMLSamlForm.txt"); 
var SAMLResposeNode = HtmlDoc.DocumentNode.SelectSingleNode("//input[@id='SAMLResponse']").ToString(); 
SAMLResposeNode = "<input id='SAMLResponse' name='SAMLResponse' type='text' runat='server' enableviewstate='False' value='" + samlAssertion + "'/>"; 

現在我只需要能夠補充說,注入標籤回原始的HTML文檔

+0

我想可能有一些有用的東西在這個問題上 http://stackoverflow.com/questions/9520932/how-do-i-use-html-agility-pack-to-edit -an-HTML的代碼段 – Pete

回答

0

好,我已經解決了這個使用以下:

HtmlAgilityPack.HtmlDocument HtmlDoc = new HtmlAgilityPack.HtmlDocument(); 
HtmlDoc.Load(@"C:\HTMLSamlForm.html"); 
var SamlNode = HtmlNode.CreateNode("<input id='SAMLResponse' name='SAMLResponse' type='text' runat='server' enableviewstate='False' value='" + samlAssertion + "'/>"); 
foreach (HtmlNode node in HtmlDoc.DocumentNode.SelectNodes("//input[@id='SAMLResponse']")) 
{ 
    string value = node.Attributes.Contains("value") ? node.Attributes["value"].Value : "&nbsp;"; 
    node.ParentNode.ReplaceChild(SamlNode, node); 
} 

然後以檢查新的HTML文件I輸出的內容,它用這樣的:

System.IO.File.WriteAllText(@"C:\SamlInjected.txt", HtmlDoc.DocumentNode.OuterHtml); 
相關問題