2016-09-21 25 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); 
相关问题