需要引用的命名空间: 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. inflate方法与findViewById的区别

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来找 re ...

  2. 《 UNIX网络编程》源码的使用

    学习编程这东西,看代码,改代码,运行代码这样才能学到实际东西!本书说在www.unpbook.com可以获取源码,不过打不开!所以google unpv13e.tar.gz 并在网络上找到了:源码:h ...

  3. 学习笔记-[Maven实战]-第三章:Maven使用入门(3)

    这里说一下在建测试工程里遇到的问题 1.第一次建工程,junit依赖始终没有成功,最后删除现在工程,新建了一个工程就好了 2.使用junit4的问题.工程默认的依赖是junit3.8.1,我改成了4. ...

  4. 如何完全卸载VS2010

    1.首先用360卸载,当卸载完成后,提示有残余的话,就强力清除 2,接着,下载IobitUninstaller工具 3.按照下面进行卸载 1.Microsoft .NET Framework 4 框架 ...

  5. git rev-list

    git-rev-list - Lists commit objects in reverse chronological order 按照时间顺序倒序排列的commit Update: If all ...

  6. Windows 7/8 自带定时关机命令

    快捷键“Windows + R”,输入cmd打开cmd.exe程序,输入以下对应命令.   两种定时关机方式: 定时任务法 输入命令“at hh:mm shutdown -s”,Enter——添加了一 ...

  7. [原]Unity3D深入浅出 - 角色控制器(Character Controller)

    角色控制器主要用于第一人称和第三人称主角的控制,并不使用刚体物理效果. 添加角色控制器的方法:依次打开菜单栏中的Component - Physiscs - Character Controller ...

  8. C# 分布式缓存服务器方案

  9. ejabberd源码流程梳理

    ejabberd的工程主要通过ejabberd.app 组织起来 ejabberd.erl : application:start(ejabberd). ejabberd_app.erl: Mod:s ...

  10. .Net 垃圾回收机制原理(二)

    英文原文:Jeffrey Richter 编译:赵玉开 链接http://www.cnblogs.com/yukaizhao/archive/2011/11/25/dot_net_GC_2.html ...