Html Agility Pack是较低的外壳cAsE SeNSiTive SVG属性

时间:2018-05-14 14:59:16

标签: .net svg html-agility-pack

viewBox(注意大写" B")是区分大小写的属性。将值更改为" viewbox"可能导致元素渲染不正确或根本不渲染。

HtmlAgilityPack将所有(X)HTML属性默认为小写。我怎么能阻止这个?

具有有效SVG元素的示例HTML页面(请注意驼峰案例viewBox属性):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link href="default.css" rel="stylesheet" type="text/css"/>
    </head>
    <body style="background-color: #000007">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 600 800">
            <rect x="0" y="0" fill="#000007" width="600" height="100%"/>
            <image width="600" height="800" xlink:href="images/cover.jpg" transform="translate(0 0)" />
        </svg>
    </body>
</html>

为了简单起见,我们可以说我想添加一个标题标签(如果缺少)。

 Private Function InsertTitleTagIfMissing(p_content As String) As String
        Dim _pageContent = p_content
        Dim _doc = New HtmlAgilityPack.HtmlDocument()
        _doc.OptionWriteEmptyNodes = True

        _doc.LoadHtml(p_content)

        Dim _head = _doc.DocumentNode.SelectSingleNode("//head")
        Dim _title = _head.SelectSingleNode("title")

        If _title Is Nothing Then
            _title = _doc.CreateElement("title")
            _head.AppendChild(_title)
            _pageContent = _doc.DocumentNode.OuterHtml
        End If

        Return _pagecontent

    End Function

    Private Sub LoadTestFile
        Dim _exampleFileName = "C:\_testPage\cover.html"
        Dim _strContents As String = String.empty


        using _objReader = New StreamReader(_exampleFileName, True)
            _strContents = _objReader.ReadToEnd()
            _objReader.close
        End Using

        _strContents = InsertTitleTagIfMissing(_strContents)

        System.Diagnostics.Debugger.Break()
    End Sub

输出(即_strContents的值)(请注意,viewbox属性现在是小写。):

<?xml version="1.0" encoding="UTF-8" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link href="default.css" rel="stylesheet" type="text/css" />
        <title></title>
    </head>
    <body style="background-color: #000007">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewbox="0 0 600 800">
            <rect x="0" y="0" fill="#000007" width="600" height="100%"></rect>
            <image width="600" height="800" xlink:href="images/cover.jpg" transform="translate(0 0)"></image>
        </svg>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

在@ccprog的帮助下,我提出的解决方案是将OptionOutputAsXml和OptionOutputOriginalCase设置为TRUE。

Private Function InsertTitleTagIfMissing(p_content As String) As String
    Dim _pageContent = p_content
    Dim _doc = New HtmlAgilityPack.HtmlDocument()
    _doc.OptionWriteEmptyNodes = True
    _doc.OptionOutputAsXml = True
    _doc.OptionOutputOriginalCase = true

    _doc.LoadHtml(p_content)

    Dim _head = _doc.DocumentNode.SelectSingleNode("//head")
    Dim _title = _head.SelectSingleNode("title")

    If _title Is Nothing Then
        _title = _doc.CreateElement("title")
        _head.AppendChild(_title)
        _pageContent = _doc.DocumentNode.OuterHtml
    End If

    Return _pagecontent

End Function