【问题标题】:.Net HTMLAgilityPack is adding empty quotes to a tag attribute.Net HTMLAgilityPack 正在向标签属性添加空引号
【发布时间】:2021-06-29 18:54:31
【问题描述】:

我目前正在使用 HMLAgilityPack 编辑 html 文件。

我的模板有这段 html:

<model-viewer class="viewer" id="viewer1" ar ar-modes="scene-viewer quick-look" ar-scale="auto" environment-image="neutral" src="mymodel.glb" camera-controls auto-rotate></model-viewer>

但是,当我编辑并保存此文档时,它会在此标记的某些部分添加一个空属性。例如,保存的 html 如下所示,其中 ar、camera-controls 和 auto-rotate 添加了空引号。

<model-viewer class="viewer" id="viewer1" ar="" ar-modes="scene-viewer quick-look" ar-scale="auto" environment-image="neutral" src="mymodel.glb" camera-controls="" auto-rotate=""></model-viewer>

如何停止

ar

在modelviewer标签变成

ar=""

?

谢谢

【问题讨论】:

    标签: c# html .net html-agility-pack


    【解决方案1】:

    https://github.com/zzzprojects/html-agility-pack/issues/422

    设置htmlDoc.GlobalAttributeValueQuote = AttributeValueQuote.WithoutValue

            var html = @"<!DOCTYPE html>
                 <html>
                 <body a b c>
                 </body>
                 </html> ";
    
            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(html);
            htmlDoc.GlobalAttributeValueQuote = AttributeValueQuote.WithoutValue;
            htmlDoc.Save(@"./test.html");
            // <!DOCTYPE html>
              //   <html>
               //  <body a b c>
               //  </body>
               // </html> 
    

    【讨论】:

    • 谢谢,看到他们允许不同的场景真的很有用。如果我使用 initial,我会得到 ar='',如果我使用 None,我会得到 ar=,根本没有引号。是否有一个选项可以删除 = 或者我会传递该属性值,然后以另一种方式自己剥离等于?再次感谢
    • 还有 AttributeValueQuote.WithoutValue 但不幸的是删除了该标签内每个属性的值,这对我也没有好处。
    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多