需要引用的命名空间: using System.Xml;

常用的类:XmlDocument、XmlElement、XmlNode、XmlNodeList

一、使用XmlDocument创建xml

 //创建XmlDocument对象
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
xmlDoc.AppendChild(dec);
//创建根节点
XmlElement root = xmlDoc.CreateElement("Books");
xmlDoc.AppendChild(root); XmlNode book = xmlDoc.CreateElement("Book");
XmlElement title = xmlDoc.CreateElement("Title");
title.InnerText = "SQL Server";
book.AppendChild(title);
XmlElement isbn = xmlDoc.CreateElement("ISBN");
isbn.InnerText = "";
book.AppendChild(isbn);
XmlElement author = xmlDoc.CreateElement("Author");
author.InnerText = "jia";
book.AppendChild(author);
XmlElement price = xmlDoc.CreateElement("Price");
price.InnerText = "";
price.SetAttribute("Unit", "_fad");
book.AppendChild(price); XmlNode book2 = xmlDoc.CreateElement("Book");
XmlElement title2 = xmlDoc.CreateElement("Title");
title2.InnerText = "C#高级编程";
book2.AppendChild(title2);
XmlElement isbn2 = xmlDoc.CreateElement("ISBN");
isbn2.InnerText = "";
book2.AppendChild(isbn2);
XmlElement author2 = xmlDoc.CreateElement("Author");
author2.InnerText = "Longsi";
book2.AppendChild(author2);
XmlElement price2 = xmlDoc.CreateElement("Price");
price2.InnerText = "";
price2.SetAttribute("Unit", "abc");
book2.AppendChild(price2); XmlElement title3 = xmlDoc.CreateElement("Title");
title3.InnerText = "我是最外面的Title";
title3.SetAttribute("name", "lxf"); root.AppendChild(book);
root.AppendChild(book2);
root.AppendChild(title3);
xmlDoc.Save(@"F:\Books.xml");
Console.WriteLine("xml文档创建成功");

结果:

<?xml version="1.0" encoding="GB2312"?>
<Books>
<Book>
<Title>SQL Server</Title>
<ISBN>444444</ISBN>
<Author>jia</Author>
<Price Unit="_fad">120</Price>
</Book>
<Book>
<Title>C#高级编程</Title>
<ISBN>88888</ISBN>
<Author>Longsi</Author>
<Price Unit="abc">1200</Price>
</Book>
<Title name="lxf">我是最外面的Title</Title>
</Books>

二、使用XmlDocument 查询xml

主要方法SelectNodes(xPath字符串)

//查询所有Title节点
XmlNodeList aa = xmlDoc.SelectNodes("//Title");

查询具有name属性的Title节点

XmlNodeList aa = xmlDoc.SelectNodes("//Title[@name]");

foreach (XmlNode item in aa)
{
Console.WriteLine(item.InnerText);
}

总结:传统的XmlDocument在创建xml上没有使用Ling to Xml简介

但在查询操作上,结合使用xPath,还是很容易很强大的。

xPath用法详见http://www.cnblogs.com/lxf1117/p/3936239.html

传统XmlDocument操作的更多相关文章

  1. C# XmlDocument操作XML

    XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...

  2. XmlDocument操作

    一.基本操作:XmlDocument 写 class Program { static void Main(string[] args) { // 使用DOM操作,常用的类:XmlDocument.X ...

  3. 利用XmlDocument操作XML文件

    利用XmlDocument可以方便的操作XML文件. .操作XML文件基本方法 ()添加对System.Xml的引用,并使用using语句添加引用: ()假设要读取的XML文件如下: <?xml ...

  4. 传统JDBC操作数据库

    package com.jdbc.example; import java.sql.Connection; import java.sql.Date; import java.sql.DriverMa ...

  5. PHP 设计模式 笔记与总结(4)PHP 链式操作的实现

    PHP 链式操作的实现 $db->where()->limit()->order(); 在 Common 下创建 Database.php. 链式操作最核心的地方在于:在方法的最后 ...

  6. Spark学习之键值对操作总结

    键值对 RDD 是 Spark 中许多操作所需要的常见数据类型.键值对 RDD 通常用来进行聚合计算.我们一般要先通过一些初始 ETL(抽取.转化.装载)操作来将数据转化为键值对形式.键值对 RDD ...

  7. 第八章 使用jQuery操作DOM

    DOM操作: jQuery中提供了一系列操作DOM强有力的方法,它们不仅简化了传统JavaScript操作DOM时繁冗的代码,更加解决了令开发者苦不堪言的跨平台浏览器的兼容. 它还让有页面元素真正动起 ...

  8. 模型层ORM操作

    一.ORM操作 1.关键性字段及参数 DateField 年月日 DateTimeField 年月日时分秒 auto_now: 每次操作改数据都会自动更新时间 auto_now_add: 新增数据的时 ...

  9. [C#] 软硬结合第二篇——酷我音乐盒的逆天玩法

    1.灵感来源: LZ是纯宅男,一天从早上8:00起一直要呆在电脑旁到晚上12:00左右吧~平时也没人来闲聊几句,刷空间暑假也没啥动态,听音乐吧...~有些确实不好听,于是就不得不打断手头的工作去点击下 ...

随机推荐

  1. javascript中实现sleep的两种方式

    最近在js中要使用到类似于C++中的sleep函数(让cpu休眠).但是js是不可能让cpu休眠,所以可以通过下面的两种方式模拟sleep函数. 方式一:使用setTimeout函数代替.如果在一个循 ...

  2. 使用程序获取整型数据和浮点型数据在内存中的表示---gyy整理

    使用程序获取整型数据和浮点型数据在内存中的表示. C++中整型(int).短整型(short int).单精度浮点数(float).双精度浮点数(double)在内存中所占字节数不同,因此取值范围也不 ...

  3. Tomcat 部署Undeployment Failure

    Tomcat 部署Undeployment Failure - yongjava的日志 - 网易博客 http://blog.163.com/qiangyongbin2000@126/blog/sta ...

  4. libevent入门教程

    首先给出官方文档吧: http://libevent.org ,首页有个Programming with Libevent,里面是一节一节的介绍libevent,但是感觉信息量太大了,而且还是英文的- ...

  5. uva 11292 Dragon of Loowater (勇者斗恶龙)

    Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...

  6. JS判断是否出现滚动条

    http://www.cnblogs.com/yazdao/archive/2010/12/06/1897742.html 该博文是想用JS检测浏览器是否出滚动条. 这边想到一个比较取巧的方法, 假如 ...

  7. bzoj列表2

    之前发过一次了,这里的题较水,没什么好讲的 bzoj1088 直接穷举前两位即可,话说程序员的扫雷是白玩的? bzoj1083 裸的最小生成树(最小生成树=最小瓶颈树),SCOI大丈夫(话说网上二分是 ...

  8. MyEclipse/Eclipse导入sun.misc.BASE64Encoder jar包步骤

    1.右键项目 -->Properties -->Java Bulid Path-> Libraries -->JRE System Library-->Access ru ...

  9. jquery图片轮播-插件

    更新内容: 1. 页面结构和css样式必定类似下边放置 2. 点击左右按钮,实现左右滑动. 3. 这一般用于多个图片轮播使用,简化并优化代码. 若因不同需求,均可自行将插件scrollimgplus. ...

  10. HDU 3567 Eight II BFS预处理

    题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...