C#操作Xml:通过XmlDocument读写Xml文档
<?xml version="1.0" encoding="utf-8" ?><students> <!--我是一段注释文字--> <student name="张平"> <courses> <course name="语文?"> <teacherComment> <![CDATA[ 这里是语文老师的批注 ]]> </teacherComment> </course> <course name="数学"> <teacherComment> <![CDATA[ 这里是数学老师的批注 ]]> </teacherComment> </course> </courses> </student></students> |
/*玉开博客 http://www.cnblogs.com/yukaizhao/ */using System;using System.Collections.Generic;using System.Text;using System.Xml;namespace XmlExample{ class Program { static void Main(string[] args) { string xmlFilePath = @"X:\about.net\example\XmlExample\1.xml"; XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //使用xpath表达式选择文档中所有的student子节点 XmlNodeList studentNodeList = doc.SelectNodes("/students/student"); if (studentNodeList != null) { foreach (XmlNode studentNode in studentNodeList) { //通过Attributes获得属性名字为name的属性 string name = studentNode.Attributes["name"].Value; Console.WriteLine("Student:" + name); //通过SelectSingleNode方法获得当前节点下的courses子节点 XmlNode coursesNode = studentNode.SelectSingleNode("courses"); //通过ChildNodes属性获得courseNode的所有一级子节点 XmlNodeList courseNodeList = coursesNode.ChildNodes; if (courseNodeList != null) { foreach (XmlNode courseNode in courseNodeList) { Console.Write("\t"); Console.Write(courseNode.Attributes["name"].Value); Console.Write("老师评语"); //通过FirstNode属性可以获得课程节点的第一个子节点,LastNode可以获得最后一个子节点 XmlNode teacherCommentNode = courseNode.FirstChild; //读取CData节点 XmlCDataSection cdata = (XmlCDataSection)teacherCommentNode.FirstChild; Console.WriteLine(cdata.InnerText.Trim()); } } } } Console.Write("\r\nPress any key to continue...."); Console.Read(); } }} |
/*玉开博客 http://www.cnblogs.com/yukaizhao/ */using System;using System.Collections.Generic;using System.Text;using System.Xml;namespace WriteXml{ class Program { static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); //创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?> xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); //创建根节点 XmlNode rootNode = xmlDoc.CreateElement("students"); //创建student子节点 XmlNode studentNode = xmlDoc.CreateElement("student"); //创建一个属性 XmlAttribute nameAttribute = xmlDoc.CreateAttribute("name"); nameAttribute .Value = "张同学"; //xml节点附件属性 studentNode.Attributes.Append(nameAttribute); //创建courses子节点 XmlNode coursesNode = xmlDoc.CreateElement("courses"); XmlNode courseNode1 = xmlDoc.CreateElement("course"); XmlAttribute courseNameAttr = xmlDoc.CreateAttribute("name"); courseNameAttr.Value = "语文"; courseNode1.Attributes.Append(courseNameAttr); XmlNode teacherCommentNode = xmlDoc.CreateElement("teacherComment"); //创建Cdata块 XmlCDataSection cdata = xmlDoc.CreateCDataSection("<font color=\"red\">这是语文老师的批注</font>"); teacherCommentNode.AppendChild(cdata); courseNode1.AppendChild(teacherCommentNode); coursesNode.AppendChild(courseNode1); //附加子节点 studentNode.AppendChild(coursesNode); rootNode.AppendChild(studentNode); //附加根节点 xmlDoc.AppendChild(rootNode); //保存Xml文档 xmlDoc.Save(@"d:\test.xml"); Console.WriteLine("已保存Xml文档"); } }} |
使用XmlDocument生成xml的要点在于使用xmlDocument的实例的CreateElement创建XmlNode或者通过CreateAttribute方法创建属性,并通过AppendChild方法附加xml节点,通过AppendAttribute附加Attribute到节点的属性集合。
C#操作Xml:通过XmlDocument读写Xml文档的更多相关文章
- XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容
一.新建Demo2类: import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPar ...
- Visual Studio 2008(C#)XML注释提取成帮助文档的方法
Visual Studio 2008(C#)XML注释提取成帮助文档的方法 1.给方法和类添加XML注释 可以手动添加,具体规则可以看MSDN:http://msdn.microsoft.co ...
- 通过XmlDocument读写Xml文档参考地址
/// <summary> /// 获取一个报表的参数 http://blog.csdn.net/hdhai9451/article/details/12170069 /// </s ...
- C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)
这篇博客将要讨论的是关于: 如何从C#的source以及注释, 生成一份Word格式的关于各个类,函数以及成员变量的说明文档. 他的大背景如下...... 最近的一个项目使用C#, 分N个模块, 在项 ...
- dom4j的读写xml文件,读写xml字符串
百度了一些博客,大同小异,在选取jar包工具的时候大概看了下,大抵是jdom原始,dom4j优秀.于是做了些练习. 参考:http://www.cnblogs.com/mengdd/archive/2 ...
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
- 使用多个项目生成Xml文件来显示帮助文档
终于到这了,我们首先将Product单独作为一个项目 WebAPI2PostMan.WebModel 并引用他,查看文档如下. 你会发现,你的注释也就是属性的描述没有了.打开App_Data/XmlD ...
- C#演示如何使用 XML 将源码编入文档
工作闲暇时间,将做工程过程中常用的代码段记录起来,下面的代码是关于C#演示如何使用 XML 将编入文档的代码,希望对大伙有较大帮助. using System; public class SomeCl ...
- 使用.NET中的XML注释(二) -- 创建帮助文档入门篇
一.摘要 在本系列的第一篇文章介绍了.NET中XML注释的用途, 本篇文章将讲解如何使用XML注释生成与MSDN一样的帮助文件.主要介绍NDoc的继承者:SandCastle. 二.背景 要生成帮助文 ...
随机推荐
- IntelliJ IDEA 问题总结之中的一个 —— jar包、assets、maven、git
因为工作须要,这几天開始弃用eclipse,换idea.用了几天,idea确实有些地方比較方便.可是麻烦也是不少.并且网上相应的资料并没有eclipse那么多,非常多都是自己琢磨解决的,所以想弄个帖子 ...
- BZOJ 1324 Exca神剑 最小割
标题效果:给定一个n*m矩阵.所有的格宝石之子,人们可选择起始位置,后除去宝石的当前位置的周围消失,然后你就可以走两步,重复上述过程 easy发现格儿子把它周围格孩子不能拿 因此,党格访问问题 黑白染 ...
- 得到View Frustum的6飞机
笔者:i_dovelemon 资源:CSDN 日期:2014 / 9 / 30 主题:View Frustum, Plane, View Matrix, Perspective Projection ...
- Swift学习资料
http://www.hangge.com/blog/cache/category_72_1.html
- 你的第一个AngularJS应用--教程二:基架、建立和測试的工具
介绍 有非常多可用的工具能够帮助你开发AngularJS 应用,那些非常复杂的框架不在我的讨论范围之中,这也是我開始这系列教程的原因. 在第一部分,我们掌握了AngularJS框架的基本结构,开发了第 ...
- sizeof运营商
sizeof它是C语言的关键字,它计算对象所占用的字节数.采用sizeof通过计算对象的大小,占用的对象是一个良好的编程习惯二手. ★你得到的内置型尺寸 一些内置类型的大小会随着体系结构的不同而不同, ...
- DOM手术台
CSS分类 排队: <div id="box" style="width:200px;border:1px solid red color:red;font-siz ...
- 程序猿常识--OJ系统和ACM测试考试大全
OJ它是Online Judge缩写系统,来在线检測程序源码的正确性. 著名的OJ有RQNOJ.URAL等. 国内著名的题库有北京大学题库.浙江大学题库等. 国外的题库包含乌拉尔大学.瓦拉杜利德大学题 ...
- Redis是新兴的通用存储系统-为何Redis要比Memcached好用
GitHub版本地址: https://github.com/cncounter/translation/blob/master/tiemao_2014/Redis_beats_Memcached/R ...
- Android Activity之间通信
package com.example.myapp; import android.app.Activity; import android.content.DialogInterface; impo ...