代码语言
.
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操作演练
作者:
asion
/ 发布于
2011/4/26
/
845
<div><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Collections.Generic; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.ComponentModel; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Data; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Drawing; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Linq; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Text; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Windows.Forms; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Xml; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.IO; </span><span style="color: rgb(0,0,255)">namespace</span><span style="color: rgb(0,0,0)"> WindowsFormsApplication1 { </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">partial</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> frm_MainForm : Form { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">string Filename = null;</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> frm_MainForm() { InitializeComponent(); } </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> btnNew_Click(</span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> sender , EventArgs e) { SaveFileDialog sf </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)"> SaveFileDialog(); sf.Title </span><span style="color: rgb(0,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)">新建XML文件 </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; sf.Filter </span><span style="color: rgb(0,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)">XML文件(*.xml)|*.xml</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; sf.ShowDialog(</span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">); myMethod_XMLwriter(sf.FileName); rtb_Editor.Text </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> File.ReadAllText(sf.FileName); } </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> myMethod_XMLwriter(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filename) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> ( filename </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)"> ) { groupBox1.Enabled </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">创建一个XML文档对象</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</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)"> XmlDocument(); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加入XML声明。</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">1.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)">UTF-8</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)">yes</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)">加入XML注释。</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlDoc.AppendChild(xmlDoc.CreateComment(</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)">加入空行。</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlDoc.AppendChild(xmlDoc.CreateWhitespace(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)"> \n</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)">创建一个根节点 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">XmlElement xmlRoot = xmlDoc.CreateElement("root"); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">创建一个根节点(带命名空间)</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlElement xmlRoot </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateElement(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">YE</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)">root</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)">http://www.smartsoso.com</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)">添加根节点</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlDoc.AppendChild(xmlRoot); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">添加【CDATA数据】。 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">xmlRoot.AppendChild(xmlDoc.CreateCDataSection("CDATA数据")); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">创建一个子节点</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlElement bgcolor </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateElement(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">bgcolor</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); bgcolor.SetAttribute (</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">ID</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)">yeye</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); bgcolor.InnerText </span><span style="color: rgb(0,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)">red</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)">子节点的值</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlRoot.AppendChild(bgcolor);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">添加到根节点中 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">保存到文件。</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlDoc.Save(filename); </span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)"> //读 //创建一个XML对象 XmlDocument myxml = new XmlDocument(); // 读取已经有的xml myxml.Load(Filename); //声明一个节点存储根节点 XmlNode movie = myxml.DocumentElement; //遍历根节点下的子节点 foreach ( XmlNode var in movie.ChildNodes ) { //Console.WriteLine(var.Name);//获取根节点的名称 //Console.WriteLine(var.InnerText);//获取根节点的值! rtb_Editor.Text += var.Name; rtb_Editor.Text += var.InnerText; } </span><span style="color: rgb(0,128,0)">*/</span><span style="color: rgb(0,0,0)"> } } } } </span></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
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
asion
贡献的其它代码
(
10
)
.
只允许运行一个窗体实例
.
C#可移动按钮“相关代码”
.
XML操作演练
.
创建快捷方式
.
自定义不规则窗体和控件
.
自定义后缀名
.
自动粘合桌面边缘
.
设置窗体效果AnimateWindow
.
全局快捷键
.
模拟QQ振屏功能
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3