How can I make a HtmlNode using HtmlGenericControl?

c# html html-agility-pack htmlgenericcontrol

Question

I'm at a loss; this must be so easy to do, but I can't understand how.

I've got this code:

 var divEl = doc.DocumentNode.SelectSingleNode("//div[@id='" + field.Id + "']");

 var newdiv = new HtmlGenericControl("div");
 newdiv.Attributes.Add("id", label.ID);
 newdiv.Attributes.Add("text", label.Text);
 newdiv.Attributes.Add("class", label.CssClass);

And I must do the following action:

    divEl.AppendChild(newdiv); //not working its expecting a HtmlNode, not a HtmlGenericControl

How can this be converted?

Thank you in advance for any answer. chapas

1
1
2/23/2012 11:00:52 AM

Accepted Answer

Check out the code below. You'll need to adjust a few things. controls Since innerHTML is nicely formed, an xmlNode may utilize it as innerXML.

XmlDocument doc = new XmlDocument();
//Load your xml here TODO
var divEl = doc.DocumentElement.SelectSingleNode("//div[@id='test']"); //Change your xpath here TODO
var newdiv = new HtmlGenericControl("div"); 
newdiv.Attributes.Add("id", "id"); 
newdiv.Attributes.Add("text", "text"); 
newdiv.Attributes.Add("class", "class");

XmlNode newNode = doc.CreateNode("NodeType", "NodeName", "URI/if/any"); //update your variables here
newNode.InnerXml = newdiv.InnerHtml;
divEl.AppendChild(newNode);

Hope this is useful.

0
2/23/2012 11:49:11 AM

Popular Answer

Why not simply build the node using the HAP API? It ought to function fairly similarly.

var newDiv = HtmlNode.CreateNode("<div/>");
newDiv.Attributes.Add("id", label.ID);
newDiv.Attributes.Add("text", label.Text);
newDiv.Attributes.Add("class", label.CssClass);

divEl.AppendChild(newDiv);

It is difficult to get the outside HTML of theHtmlGenericControl instance (AFAIK). You could simply enter the HTML into the if you had it.HtmlNode.CreateNode() a way to make it. But I strongly advise against attempting to make it work.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow