文本输出的时候需要将空格或回车等字符替换成html标签,在这里我从xml文档中读取文本,文本本身存储的时候是带格式的:xml结构如下:
<?xml version="1.0" encoding="utf-8"?>
<jgxy>
<news>
<news_title>新闻标题</news_title>
<news_author>新闻作者</news_author>
<news_date>日期</news_date>
<news_source>来源</news_source>
<news_content>新闻内容……</news_content>
</news>
下面是.cs中的代码:
protected void Page_Load(object sender, EventArgs e)
{
string url = Server.MapPath("~/App_Data/News.xml");//打开xml文档
XmlDocument doc = new XmlDocument();//建立DOM文档
doc.Load(url);
XmlNodeList nodelist = doc.GetElementsByTagName("news_content");//获得“news_content”节点集合
XmlNode node = nodelist.Item(0);//我显示的是第一个节点新闻内容
string text = node.FirstChild.Value;
Response.Write(this.MyReplace(text));
}
public string MyReplace(string mystr)//字符替换函数
{
if (mystr + "a" == "a")//判断是否是空格
{
return (" ");
}
else//判断回车
{
mystr=mystr.Replace("\n\r","<br>");
mystr = mystr.Replace("\r", "<br>");
mystr = mystr.Replace("\t"," ");
return(mystr);
}
}