首页 > 解决方案 > .Net HTMLAgilityPack 正在向标签属性添加空引号

问题描述

我目前正在使用 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

在模型查看器标签中成为

ar=""

?

谢谢

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

解决方案


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

Set 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> 

推荐阅读