需要引用的命名空间: 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. MyBatis的动态SQL操作--查询

    查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,

  2. 从今天起,正式步入cnblogs,向曾经的脚印说声对不起!

    步入这个行业也好多年了,从来没有定居过一个地方. 看过很多前辈们留下的资料,对后者门(其中还有我)留下很多珍贵的东西. 所以,我要向前辈学习,壮大自己,在学习的同时,不要忘记帮助别人. 对曾经我留下的 ...

  3. 中文编码之GB2312,Big5,GBK简介

    汉字编码中现在主要用到的有三类,包括GBK,GB2312和Big5. 1.GB2312 又称国标码,由国家标准总局发布,1981年5月1日实施,通行于大陆.新加坡等地也使用此编码.它是一个简化字的编码 ...

  4. python web开发遇到socket.error[errno 10013]

    socket.error[errno 10013],端口被占用 重新换一个端口,或者把占用该端口的程序关闭就可以了

  5. Java面试题-并发容器和框架

    1. 如何让一段程序并发的执行,并最终汇总结果? 答:使用CyclicBarrier 和CountDownLatch都可以,使用CyclicBarrier 在多个关口处将多个线程执行结果汇总,Coun ...

  6. tap,touch,touchstart,事件与click事件的区别

    根据源码所见, 移动端为了将将单击事件更加灵敏,所以现在的JQM,ST...框架都将JS单击事件封装成tap,或者touch或者touchstart事件, 其实现本质是将click触发多次,以打成移动 ...

  7. Self-Paced Training (2) - Docker Fundamentals

    Agenda- Building Images Dockerfile Managing Images and Containers Distributing Images on Docker Hub ...

  8. [POJ 2429] GCD & LCM Inverse

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: ...

  9. UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)

    题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...

  10. VC++6.0环境下调试c语言代码的方法和步骤_附图

    1.C语言程序四步开发步骤 (1)编辑.可以用任何一种编辑软件将在纸上编写好的C语言程序输入计算机,并将C语言源程序文件*.c以纯文本文件形式保存在计算机的磁盘上(不能设置字体.字号等). (2)编译 ...