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. 二.背景 要生成帮助文 ...
随机推荐
- 认识Backbone (一)
Backbone.js为复杂WEB应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和自定义事件:集合附有可枚举函数的丰富API: 视 ...
- 获取activity的根视图
Activity的根视图是什么? Activity所谓的根视图,就是Activity的最底层的View,也就是在Acitivty创建的时候setContentView的时候传入的View. 如何获取到 ...
- Netty+Tomcat热部署端口占用解决办法(转)
在eclipse使用maven deploy (tomcat:deploy) 热部署netty项目 ,项目启动的时候会报错端口被占用. java.net.BindException: Address ...
- POJ 3177 Redundant Paths - from lanshui_Yang
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- cocos2d-x 3.1.1 学习笔记[21]cocos2d-x 创建过程
文章出自于 http://blog.csdn.net/zhouyunxuan RootViewController.h #import <UIKit/UIKit.h> @interfac ...
- Yaha,Yaho
Yaha: Yaho: 听雪楼上听雪落,雪落无声空余楼. 同样的地方,一坐三年多,人走楼空,回顾空留. 自己非常白痴地画着苹果,非常嗨森地逗自己玩. 这两层精致的书库是大学里面能容纳我的地方(ABC的 ...
- Windows创建的基本含义和进程的进程的内核
过程 1 这意味着过程: 1.1 一个是在操作系统的内核对象管理处理. 的统计信息的地方. 1.2 还有一个是地址空间.它包括全部可运行模块或DL L 模块的代码和数据.它还包括动态内存分配的 ...
- Html5 拖放上传图片
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- Socket tips: UDP Echo service - Client code
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/soc ...
- 对LevelDB的“升级版”存储引擎RocksDB的调研成果
Google的leveldb是个非常优秀的存储引擎.但还是有一些不尽人意的地方,比方leveldb不支持多线程合并.对key范围查找的支持还非常easy,未做优化措施,等等.而Facebook的Roc ...