代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
解析xml字符串
作者:
chenqingwei
/ 发布于
2011/5/4
/
1071
<
->
<
>
->
>
"
->
"
'
->
'
&
->
&
<div> <p style="margin: 0px 0px 14px"><strong>1. 利用string.Replace() 五次替换</strong> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:02556ec6-11a3-46bb-bef2-be395171f26c"> <pre style="background-color: white; margin: 0px 0px 0px 22px; font-size: 1em; overflow: auto"><div><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xml </span><span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><node>it's my \"node\" & i like it<node></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;encodedXml </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xml.Replace(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">&</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">&</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">).Replace(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">).Replace(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">).Replace(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">\"</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">).Replace(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> RESULT: <node>it's my "node" & i like it<node></span></div></pre> </div> <p style="margin: 0px 0px 14px"> <p style="margin: 0px 0px 14px"><strong>2. 利用System.Web.HttpUtility.HtmlEncode() 方便 </strong> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3971b22a-7fb5-4cba-9c91-adaee968f817"> <pre style="background-color: white; margin: 0px 0px 0px 22px; font-size: 1em; overflow: auto"><div><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xml </span><span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><node>it's my \"node\" & i like it<node></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> encodedXml </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> HttpUtility.HtmlEncode(xml);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> RESULT: <node>it's my "node" & i like it<node></span></div></pre> </div> <p style="margin: 0px 0px 14px"><strong>3. 利用System.Security.SecurityElement.Escape() 不常用 </strong> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:bccf00e9-a444-40c6-8512-b960d475307e"> <pre style="background-color: white; margin: 0px 0px 0px 22px; font-size: 1em; overflow: auto"><div><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xml </span><span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><node>it's my \"node\" & i like it<node></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> encodedXml </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> System.Security.SecurityElement.Escape(xml);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> RESULT: <node>it's my "node" & i like it<node></span></div></pre> </div> <p style="margin: 0px 0px 14px"> <p style="margin: 0px 0px 14px"><strong>4. 利用 System.Xml.XmlTextWriter </strong> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:9545f675-57d9-49c0-bded-0a5ff9bf7182"> <pre style="background-color: white; margin: 0px 0px 0px 22px; font-size: 1em; overflow: auto"><div><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xml </span><span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"><node>it's my \"node\" & i like it<node></span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> (XmlTextWriter xtw </span><span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlTextWriter(</span><span style="color: rgb(128,0,0)">@"</span><span style="color: rgb(128,0,0)">c:\xmlTest.xml</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, Encoding.Unicode)){xtw.WriteStartElement(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">xmlEncodeTest</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">);xtw.WriteAttributeString(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">testAttribute</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, xml);xtw.WriteString(xml);xtw.WriteEndElement();}</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)"> RESULT:</span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)"><xmlEncodeTest testAttribute="<node>it's my "node" & i like it<node>"><node>it's my "node" & i like it<node></xmlEncodeTest></span><span style="color: rgb(0,128,0)">*/</span></div></pre> </div> </div>
试试其它关键字
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
chenqingwei
贡献的其它代码
(
18
)
.
ChineseCalendar类
.
利用任务计划定时执行SQL语句
.
获取文件的安全信息
.
获取收藏夹目录到树型控件
.
解析xml字符串
.
产生五位随机数
.
反射类工具类
.
字符串工具类
.
实现编号自动增长
.
要求是取得每个ID对应postId的前三条
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3