【问题标题】:Removing DIV from a text file if it contains a certain classname如果包含某个类名,则从文本文件中删除 DIV
【发布时间】:2012-06-24 19:23:21
【问题描述】:

我目前正在处理一个内部包含 RSS 提要的 XML 文档。我想解析它,以便如果找到一个类名为“feedflare”的 div 标签,代码将删除整个 DIV。

我找不到执行此操作的示例,因为对它的搜索被“HTML 编辑器错误”和其他不相关的数据污染了。

这里有人愿意分享实现我目标的方法吗?

如果可以避免的话,我必须声明我不想使用 HtmlAgilityPack

这是我的流程:

加载 XML,解析元素并挑选出标题、描述、链接。 然后将所有这些保存为 HTML(以编程方式添加标签以构建网页),然后添加所有标签后,我想解析生成的“HTML 文本”并删除烦人的 DIV 标签。

让我们假设“string HTML = textBox1.text”,其中 textBox1 是在解析主 XML 文档后粘贴生成的 HTML 的位置。

然后我将如何遍历 textBox1.text 的内容并仅删除名为“feedflare”的 div 标签(见下文)。

<div class="feedflare">
<a href="http://feeds.gawker.com/~ff/kotaku/full?a=lB-zYAGjzDU:1zqeSgzxt90:yIl2AUoC8zA">
<img src="http://feeds.feedburner.com/~ff/kotaku/full?d=yIl2AUoC8zA" border="0"></img></a> 
<a href="http://feeds.gawker.com/~ff/kotaku/full?a=lB-zYAGjzDU:1zqeSgzxt90:H0mrP-F8Qgo">
<img src="http://feeds.feedburner.com/~ff/kotaku/full?d=H0mrP-F8Qgo" border="0"></img></a> 
<a href="http://feeds.gawker.com/~ff/kotaku/full?a=lB-zYAGjzDU:1zqeSgzxt90:D7DqB2pKExk">
<img src="http://feeds.feedburner.com/~ff/kotaku/full?i=lB-zYAGjzDU:1zqeSgzxt90:D7DqB2pKExk" border="0"></img></a> 
<a href="http://feeds.gawker.com/~ff/kotaku/full?a=lB-zYAGjzDU:1zqeSgzxt90:V_sGLiPBpWU">
<img src="http://feeds.feedburner.com/~ff/kotaku/full?i=lB-zYAGjzDU:1zqeSgzxt90:V_sGLiPBpWU" border="0"></img></a>
</div>

提前谢谢你。

【问题讨论】:

  • 你想只删除div标签还是&lt;div&gt;&lt;/div&gt;之间的所有内容?
  • @harry180 如果您阅读帖子的第一段,它会说the code would remove the whole DIV
  • 解释一下您为什么不想使用 HtmlAgilityPack 可能会有所帮助。有一个完整的例子也会很有帮助。

标签: c# html wpf xml tags


【解决方案1】:

使用this xml library,执行:

XElement root = XElement.Load(file); // or .Parse(string);
XElement div = root.XPathElement("//div[@class={0}]", "feedflare");
div.Remove();
root.Save(file); // or string = root.ToString();

【讨论】:

  • 感谢您的回复。但是,我得到一个 NULL 异常(System.NullReferenceException 未处理 Message=Object reference not set to an instance of an object.)运行此代码:code1 XElement root = XElement.Parse(textBox1.Text); XElement div = root.XPathElement("//div[@class={0}]", "feedflare"); div.Remove(); &lt;--- Exception thrown here string test = root.ToString(); MessageBox.Show(test);code1
  • 我在哪里可以得到你的完整 xml 来测试它?
【解决方案2】:

试试这个

   System.Xml.XmlDocument d = new System.Xml.XmlDocument();
   d.LoadXml(Your_XML_as_String);
    foreach(System.Xml.XmlNode n in d.GetElementsByTagName("div"))
   d.RemoveChild(n);

并使用d.OuterXml 检索新的xml。

【讨论】:

    【解决方案3】:

    我在 Javascript 中的解决方案是:

    function unrichText(texto) {
      var n = texto.indexOf("\">"); //Finding end of "<div&nbsp;class="ExternalClass...">
      var sub = texto.substring(0, n+2); //Adding first char and last two (">)
      var tmp = texto.replace(sub, ""); //Removing it
      tmp = replaceAll(tmp, "</div>", ""); //Removing last "div"
      tmp = replaceAll(tmp, "<p>", ""); //Removing other stuff
      tmp = replaceAll(tmp, "</p>", "");
      tmp = replaceAll(tmp, "&#160;", "");
      return (tmp);
    }
    
    function replaceAll(str, find, replace) {
        return str.replace(new RegExp(find, 'g'), replace);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      相关资源
      最近更新 更多