您的位置:首页 > 数据库

带格式存储的文本从数据库或xml中的输出

2008-07-26 12:22 267 查看
文本输出的时候需要将空格或回车等字符替换成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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐