开发者

Build HtmlGenericControl from a string of full html

I want to be able to add attributes to a string of html without having to build a parser to handle th开发者_运维百科e html. In one specific case, I want to be able to extract the id of the html or insert an id to the html server side.

Say I have:

string stringofhtml = "<img src=\"someimage.png\" alt=\"the image\" />";

I would like to be able to do something like:

HtmlGenericControl htmlcontrol = new HtmlGenericControl(stringofhtml);
htmlcontrol.Attributes["id'] = "newid";

OR

int theid = htmlcontrol.Attributes["id"];

This is just a way that I can access/add attributes of the html strings that I have.


You can do this:

HtmlGenericControl ctrl = new HtmlGenericControl();
ctrl.InnerHtml = "<img src=\"someimage.png\" alt=\"the image\" />"; 

You could always use a LiteralControl too, instead of an HtmlGenericControl:

LiteralControl lit = new LiteralControl(stringOfHtml);


I do not think there is a control available which will provide you with the functionality you are looking for.

Below I have made use of the HtmlAgility pack to parse/query the HTML and created a new control subclassing the Literal control.

This control accepts an HTML string, checks to ensure it contains at least a single element and provides access to get/set that elements attributes.

Example usage

string image = "<img src=\"someimage.png\" alt=\"the image\" />";

HtmlControlFromString htmlControlFromString = new HtmlControlFromString(image);

htmlControlFromString.Attributes["id"] = "image2";

string id = htmlControlFromString.Attributes["id"];

Control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using HtmlAgilityPack;

public class HtmlControlFromString : Literal
{
    private HtmlDocument _document = new HtmlDocument();
    private HtmlNode _htmlElement;

    public AttributesCollection Attributes { get; set; }

    public HtmlControlFromString(string html)
    {
        _document.LoadHtml(html);
        if (_document.DocumentNode.ChildNodes.Count > 0)
        {
            _htmlElement = _document.DocumentNode.ChildNodes[0];

            Attributes = new AttributesCollection(_htmlElement);
            Attributes.AttributeChanged += new EventHandler(Attributes_AttributeChanged);

            SetHtml();
        }
        else
        {
            throw new InvalidOperationException("Argument does not contain a valid html element.");
        }
    }

    void Attributes_AttributeChanged(object sender, EventArgs e)
    {
        SetHtml();
    }

    void SetHtml()
    {
        Text = _htmlElement.OuterHtml;
    }
}

public class AttributesCollection
{
    public event EventHandler AttributeChanged;

    private HtmlNode _htmlElement;

    public string this[string attribute]
    {
        get
        {
            HtmlAttribute htmlAttribute = _htmlElement.Attributes[attribute];
            return htmlAttribute == null ? null : htmlAttribute.Value;
        }
        set
        {
            HtmlAttribute htmlAttribute = _htmlElement.Attributes[attribute];
            if (htmlAttribute == null)
            {
                htmlAttribute = _htmlElement.OwnerDocument.CreateAttribute(attribute);
                htmlAttribute.Value = value;
                _htmlElement.Attributes.Add(htmlAttribute);
            }
            else
            {
                htmlAttribute.Value = value;
            }

            EventHandler attributeChangedHandler = AttributeChanged;
            if (attributeChangedHandler != null)
                attributeChangedHandler(this, new EventArgs());
        }
    }

    public AttributesCollection(HtmlNode htmlElement)
    {
        _htmlElement = htmlElement;
    }
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜