English 中文(简体)
changing a node type to #text whilst keeping the innernodes with the HtmlAgilityPack
原标题:

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. ;-)

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签