C# 在html中查找正文结尾标记的最佳方法
我正在编写一个程序,向html文件中添加一些代码 我打算使用一系列的indexof和循环来找到本质上是“X”的东西 (其中X是我要找的地点) 我突然想到,也许有一种更雄辩的方法可以做到这一点 有人有什么建议吗 它现在看起来像什么C# 在html中查找正文结尾标记的最佳方法,c#,.net,html,winforms,indexof,C#,.net,Html,Winforms,Indexof,我正在编写一个程序,向html文件中添加一些代码 我打算使用一系列的indexof和循环来找到本质上是“X”的东西 (其中X是我要找的地点) 我突然想到,也许有一种更雄辩的方法可以做到这一点 有人有什么建议吗 它现在看起来像什么 <body onLoad="JavaScript:top.document.title='Abraham L Barbrow'; if (self == parent) document.getElementById('divFrameset').style.di
<body onLoad="JavaScript:top.document.title='Abraham L Barbrow'; if (self == parent) document.getElementById('divFrameset').style.display='block';">
当我完成时它应该是什么样子
var gaJsHost=((“https:==document.location.protocol)?”https://ssl." : "http://www.");
write(unescape(“%3Cscript src=”+gaJsHost+“google analytics.com/ga.js”type='text/javascript'%3E%3C/script%3E”);
试一试{
var pageTracker=_gat._getTracker(“UA-9xxxxxx-1”);
页面跟踪器。_trackPageview();
}捕获(错误){}
我建议使用将html解析为DOM并使用它。您可能想看看如何使用html敏捷包
如果HTML文件是有效的XHTML,则始终可以使用XmlDocument类对其进行解释。然后,您可以轻松地查找body元素并向其附加子元素。这会将元素放在结束标记的正前方。我不确定我是否理解您的意思,但您的意思是
// Given an HTML document in "htmlDocument", and new content in "newContent"
string newHtmlDocument = htmlDocument.Replace("</body>", newContent+"</body>");
//在“htmlDocument”中提供了一个HTML文档,在“newContent”中提供了新内容
字符串newHtmlDocument=htmlDocument.Replace(“,newContent+”);
很明显我不知道c#。。。您可能希望通过regexps使“body”标记不区分大小写。我不确定您要在标记后添加的示例内容是否正确,但如果正确,我发现两个问题:
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("</body>", CreateCode(textBox1.Text) + "</body>");
using (StreamWriter sw = new StreamWriter(filename))
{
sw.Write(Htmlcode);
}
}
}
private string CreateCode(string Number)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("<script type=\"text/javascript\">");
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("<//script>");
sb.AppendLine("<script type=/\"text//javascript/\">");
sb.AppendLine("try {");
sb.AppendLine(string.Format("var pageTracker = _gat._getTracker(/\"{0}/\");", Number));///"UA-9909000-1"
sb.AppendLine("pageTracker._trackPageview();");
sb.AppendLine("} catch(err) {}<//script>");
sb.AppendLine();
return sb.ToString();
}
}
private void按钮1\u单击(对象发送者,事件参数e)
{
OpenFileDialog OFD=新建OpenFileDialog();
OFD.Multiselect=true;
OFD.Filter=“HTML文件(*.htm*)|*.htm*|”+
“所有文件(*.*)|*.*”;
if(OFD.ShowDialog()==DialogResult.OK)
{
foreach(OFD.filename中的字符串s)
{
控制台。写入线(s);
AddAnalytics(s);
}
MessageBox.Show(“完成!”);
}
}
私有void AddAnalytics(字符串文件名)
{
字符串Htmlcode=“”;
使用(StreamReader sr=新StreamReader(文件名))
{
Htmlcode=sr.ReadToEnd();
}
如果(!Htmlcode.Contains(textBox1.Text))
{
Htmlcode=Htmlcode.Replace(“,CreateCode(textBox1.Text)+”);
使用(StreamWriter sw=新StreamWriter(文件名))
{
sw.Write(Htmlcode);
}
}
}
私有字符串CreateCode(字符串编号)
{
StringBuilder sb=新的StringBuilder();
(某人);
某人加上一行(“”);
sb.AppendLine(“var gaJsHost=(\“https:\”==document.location.protocol)?\”https://ssl.\" : \"http://www.\");");
sb.AppendLine(“document.write(unescape(\%3Cscript src='\'”+gaJsHost+\'googleanalytics.com/ga.js'));
某人加上一行(“”);
某人加上一行(“”);
某人追加(“try{”);
sb.AppendLine(string.Format(“var pageTracker=\u gat.\u getTracker(/\“{0}/\”);”,Number));///“UA-9909000-1”
sb.AppendLine(“页面跟踪器._trackPageview();”);
某人附加物(“}抓(错){}”);
(某人);
使某人返回字符串();
}
}
我认为这可能就像用大炮打苍蝇一样。我想它的html很繁琐。如果我要在windows中搜索,它只会是indexof(“”);但索引不支持wild Cards DOM操作确实是最安全的方法,crash893。对对象的HTML序列化使用字符串操作就像使用锤子绘制蓝图一样。我可以在没有DOM的情况下用17行代码来完成这项工作,这可能更安全,但就像驾驶坦克去教堂一样我一开始就不需要那么安全在HTML中,body元素的end标记是可选的,所以除非你想处理那些显式包含标记的页面子集……只要使用一个合适的解析器!或者你可以像其他答案一样使用整个DOM解析器。但是如果你只需要这样做,为什么还要加载额外的垃圾呢?这正是我想要做的,而不是add在结束体标记之前,我想在开始标记之后添加它。我的问题是,体标记并不总是具有属性。在这种情况下,您可能需要使用一种DOM解析器解决方案。您可以将开始体标记与正则表达式匹配,但并非在所有情况下都能完美地工作。这是我的经验……我想所有的我真的需要做的是查找“”,但我不知道如何做才能清除我想在标记后添加代码
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("</body>", CreateCode(textBox1.Text) + "</body>");
using (StreamWriter sw = new StreamWriter(filename))
{
sw.Write(Htmlcode);
}
}
}
private string CreateCode(string Number)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("<script type=\"text/javascript\">");
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("<//script>");
sb.AppendLine("<script type=/\"text//javascript/\">");
sb.AppendLine("try {");
sb.AppendLine(string.Format("var pageTracker = _gat._getTracker(/\"{0}/\");", Number));///"UA-9909000-1"
sb.AppendLine("pageTracker._trackPageview();");
sb.AppendLine("} catch(err) {}<//script>");
sb.AppendLine();
return sb.ToString();
}
}
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;
}