热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在html中找到body标记结束的最佳方法-bestwaytofindendofbodytaginhtml

Imwritingaprogramtoaddsomecodetohtmlfiles我正在编写一个程序来为html文件添加一些代码Iwasgoingtouseas

I'm writing a program to add some code to html files

我正在编写一个程序来为html文件添加一些代码

I was going to use a series of indexof and loops to find what is essentially ""X (where X is the spot im looking for)

我打算使用一系列indexof和循环来找到本质上是“X”(其中X是我正在寻找的地方)

It occurred to me that there might be a more eloquent way of doing this

在我看来,可能有一种更有说服力的方式来做到这一点

does anyone have any suggestions.

有没有人有什么建议。

what it looks like currently

它目前的样子


what it should look like when im done

我完成后应该是什么样子





7 个解决方案

#1


I'm not sure I'm understanding you, but do you mean this?

我不确定我是否理解你,但你的意思是这个吗?

// Given an HTML document in "htmlDocument", and new content in "newContent"
string newHtmlDocument = htmlDocument.Replace("", newContent+"");

And it's probably obvious I don't know c#... You'd probably want to make the "body" tag case insensitive via regexps.

而且很明显我不知道c#...你可能想通过regexp使“body”标签不区分大小写。

#2


I would recommend to use HtmlAgilityPack to parse the html into DOM and work with it.

我建议使用HtmlAgilityPack将html解析为DOM并使用它。

#3


public string AddImageLink(string emailBody,string imagePath)
{
    try
    {
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(emailBody);

    HtmlNode node = doc.DocumentNode.SelectSingleNode("//body");

    // get body using xpath query ("//body")
    // create the new node ..

    HtmlNodeCollection LinkNode = new HtmlNodeCollection(node);
    //

    HtmlNode linkNode = new HtmlNode(HtmlNodeType.Element,doc,0);
    linkNode.Name = "A";
    linkNode.Attributes.Add("href","www.splash-solutions.co.uk");


    HtmlNode imgNode = new HtmlNode(HtmlNodeType.Element,doc,1);
    imgNode.Name = "img";
    imgNode.Attributes.Add("src",imagePath);

    //appending the linknode with image node
    linkNode.AppendChild(imgNode);

    LinkNode.Append(linkNode);

    //appending LinkNode to the body of the html
    node.AppendChildren(LinkNode);


    StringWriter writer = new StringWriter();
    doc.Save(writer);
    emailBody = writer.ToString();
    return emailBody;
}

#4


If the HTML files are valid XHTML you could always use the XmlDocument class to interpret it. You could then easily look for the body element and append a child element to it. This would place the element right before the closing tag.

如果HTML文件是有效的XHTML,您可以始终使用XmlDocument类来解释它。然后,您可以轻松查找body元素并向其添加子元素。这会将元素放在结束 标记之前。

#5


You might want to look at using the Html Agility Pack

您可能希望查看使用Html Agility Pack

http://www.codeplex.com/htmlagilitypack

#6


I'm not sure whether the example content you want to add after the tag is the correct one or not, but if it is, I'm seeing two problems:

我不确定您要在标签之后添加的示例内容是否正确,但如果是,我会看到两个问题:

  1. The Google Analytics code should be added just before the end tag, not the opening tag. That ensures that you don't have to wait for it to load before loading your own code.
  2. 应在结束标记之前添加Google Analytics代码,而不是在开始标记之前添加。这可确保您在加载自己的代码之前不必等待它加载。

  3. If you're adding some other Javascript, why not add that in an external file, and execute that one onload instead?
  4. 如果你要添加一些其他的Javascript,为什么不在外部文件中添加它,并执行那个onload呢?

Hope that's of some help :)

希望有一些帮助:)

#7


This is what i got

这就是我得到的

feel free to make suggestions

随意提出建议

 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog();
            OFD.Multiselect = true;
            OFD.Filter = "HTML Files (*.htm*)|*.HTM*|" +
          "All files (*.*)|*.*";

            if (OFD.ShowDialog() == DialogResult.OK)
            {
                foreach (string s in OFD.FileNames)
                {
                    Console.WriteLine(s);
                    AddAnalytics(s);
                }
                MessageBox.Show("done!");
            }
        }
        private void AddAnalytics(string filename)
        {

            string Htmlcode = "";
            using (StreamReader sr = new StreamReader(filename))
            {
                Htmlcode = sr.ReadToEnd();
            }
            if (!Htmlcode.Contains(textBox1.Text))
            {
                Htmlcode = Htmlcode.Replace("", CreateCode(textBox1.Text) + "");

                using (StreamWriter sw = new StreamWriter(filename))
                {
                    sw.Write(Htmlcode);
                }
            }
        }

        private string CreateCode(string Number)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendLine("");
            sb.AppendLine("var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");");
            sb.AppendLine("document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' ");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("try {");
            sb.AppendLine(string.Format("var pageTracker = _gat._getTracker(/\"{0}/\");", Number));///"UA-9909000-1"
            sb.AppendLine("pageTracker._trackPageview();");
            sb.AppendLine("} catch(err) {}");
            sb.AppendLine();
            return sb.ToString();
        }
    }

推荐阅读
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
author-avatar
秘密不能说雨
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有