views:

98

answers:

1

I'm using the HtmlAgilityPack to parse an XML file that I'm converting to HTML. Some of the nodes will be converted to an HTML equivalent. The others that are unnecessary I need to remove while maintaining the contents. I tried converting it to a #text node with no luck. Here's my code:

private HtmlNode ConvertElementsPerDatabase(HtmlNode parentNode, bool transformChildNodes)
{
    var listTagsToReplace = XmlTagMapping.SelectAll(string.Empty);  // Custom Dataobject
    var node = parentNode;
    if (node != null)
    {
        var bNodeFound = false;
        if (node.Name.Equals("xref"))
        {
            bNodeFound = true;
            node = NodeXref(node);
        }
        if (node.Name.Equals("graphic"))
        {
            bNodeFound = true;
            node = NodeGraphic(node);
        }
        if (node.Name.Equals("ext-link"))
        {
            bNodeFound = true;
            node = NodeExtLink(node);
        }

        foreach (var infoTagToReplace in listTagsToReplace)
        {
            if (node.Name.Equals(infoTagToReplace.XmlTag))
            {
                bNodeFound = true;
                node.Name = infoTagToReplace.HtmlTag;
                if (!string.IsNullOrEmpty(infoTagToReplace.CssClass))
                    node.Attributes.Add("class", infoTagToReplace.CssClass);

                if (node.HasAttributes)
                {
                    var listTagAttributeToReplace = XmlTagAttributeMapping.SelectAll_TagId(infoTagToReplace.Id); // Custom Dataobject
                    for (int i = 0; i < node.Attributes.Count; i++ )
                    {
                        var bDeleteAttribute = true;
                        foreach (var infoTagAttributeToReplace in listTagAttributeToReplace)
                        {
                            if (infoTagAttributeToReplace.XmlName.Equals(node.Attributes[i].Name))
                            {
                                node.Attributes[i].Name = infoTagAttributeToReplace.HtmlName;
                                bDeleteAttribute = false;
                            }
                        }
                        if (bDeleteAttribute)
                            node.Attributes.Remove(node.Attributes[i].Name);
                    }
                }
            }
        }
        if (transformChildNodes)
            for (int i = 0; i < parentNode.ChildNodes.Count; i++)
                parentNode.ChildNodes[i] = ConvertElementsPerDatabase(parentNode.ChildNodes[i], true);

        if (!bNodeFound)
        {
            // Replace with #text
        }
    }
    return parentNode;
}

At the end I need to do the node replacement (where you see the "Replace with #text" comment) if the node is not found. I've been ripping my hair (what's left of it) out all day and it's probably something silly. I'm unable to get the help to compile and there is no online version. Help Stackoverflow! You're my only hope. ;-)

A: 

I would think you could just do this:

return new HtmlNode(HtmlNodeType.Text, parentNode.OwnerDocument, 0);

This of course adds the node to the head of the document, but I assume you have some sort of code in place to handle where in the document the node should be added.

Regarding the documentation comment, the current (as of this writing) download of the Html Agility Pack documentation contains a CHM file which doesn't require compilation in order to view.

casperOne
yeah, I got that...unfortunately all the pages in it say "file not found"...though there is a index...
craigmoliver