没有效果的HTML标记

时间:2014-01-04 00:14:06

标签: c# .net

示例:

输入:This is sample text - 在文本框中选择此文本

现在我点击button1并:

输出:<b>This is sample text</b> - 这不是粗体。我只想要没有效果的原始html标签。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using HtmlAgilityPack;

namespace program1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://url.com/";
            HtmlWeb web = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument htmldoc = web.Load(url);
            htmldoc.OptionFixNestedTags = true;

            HtmlNode node = htmldoc.DocumentNode.SelectSingleNode("//h1[@class='class']");
            string title = node.InnerText;
            node = htmldoc.DocumentNode.SelectSingleNode("//div[@class='class2']");
            string content = node.InnerText;
            richTextBox1.Text = content;
            textBox2.Text = title;
        }
    }
}

我的语言不好然后我画了我的意思..但我需要10点声望才能发布图片。遗憾。

2 个答案:

答案 0 :(得分:1)

在按钮1中单击,您可以执行以下操作:

string newText = String.Format("<b>{0}</b>",textBox1.SelectedText);
textBox1.Text = textBox1.Replace(textBox1.SelectedText, newText);

我认为它是WinForms

答案 1 :(得分:0)

这主要是工作http://jsfiddle.net/tx3Gr/

HTML

<div id="mytext">Hello There</div><button>click me</button>

Javascript :(使用jquery)

function safe_tags(str) {
    return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;') ;
}

$('button').on('click',
function(){
        var txt = $('#mytext').text();

       var txt1= safe_tags('<b>'+txt+'</b>');

    $('#mytext').html(txt1)
});