传统XmlDocument操作
需要引用的命名空间: 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操作的更多相关文章
- C# XmlDocument操作XML
XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...
- XmlDocument操作
一.基本操作:XmlDocument 写 class Program { static void Main(string[] args) { // 使用DOM操作,常用的类:XmlDocument.X ...
- 利用XmlDocument操作XML文件
利用XmlDocument可以方便的操作XML文件. .操作XML文件基本方法 ()添加对System.Xml的引用,并使用using语句添加引用: ()假设要读取的XML文件如下: <?xml ...
- 传统JDBC操作数据库
package com.jdbc.example; import java.sql.Connection; import java.sql.Date; import java.sql.DriverMa ...
- PHP 设计模式 笔记与总结(4)PHP 链式操作的实现
PHP 链式操作的实现 $db->where()->limit()->order(); 在 Common 下创建 Database.php. 链式操作最核心的地方在于:在方法的最后 ...
- Spark学习之键值对操作总结
键值对 RDD 是 Spark 中许多操作所需要的常见数据类型.键值对 RDD 通常用来进行聚合计算.我们一般要先通过一些初始 ETL(抽取.转化.装载)操作来将数据转化为键值对形式.键值对 RDD ...
- 第八章 使用jQuery操作DOM
DOM操作: jQuery中提供了一系列操作DOM强有力的方法,它们不仅简化了传统JavaScript操作DOM时繁冗的代码,更加解决了令开发者苦不堪言的跨平台浏览器的兼容. 它还让有页面元素真正动起 ...
- 模型层ORM操作
一.ORM操作 1.关键性字段及参数 DateField 年月日 DateTimeField 年月日时分秒 auto_now: 每次操作改数据都会自动更新时间 auto_now_add: 新增数据的时 ...
- [C#] 软硬结合第二篇——酷我音乐盒的逆天玩法
1.灵感来源: LZ是纯宅男,一天从早上8:00起一直要呆在电脑旁到晚上12:00左右吧~平时也没人来闲聊几句,刷空间暑假也没啥动态,听音乐吧...~有些确实不好听,于是就不得不打断手头的工作去点击下 ...
随机推荐
- TCP长连接与短连接的区别
http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html 1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,se ...
- 睡眠--TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE
http://i.cnblogs.com/EditPosts.aspx?opt=1 Two states are associated with sleeping, TASK_INTERRUPTI ...
- ActiveMQ之 TCP通讯机制
ActiveMQ支持多种通讯协议TCP/UDP等,我们选取最常用的TCP来分析ActiveMQ的通讯机制.首先我们来明确一个概念: 客户(Client):消息的生产者.消费者对ActiveMQ来说都 ...
- python学习笔记八--动态类型
一.变量,对象,引用: 1. 变量: 2. 对象:均包含了一个头部信息,有以下两部分内容 a.对象的数据类型, b.引用计数器:纪录当前引用货指向该对象的数量,一旦计数器被清零,该对象的内存空间就会被 ...
- ServletContext对象
**1 ServletContext对象 1)在web应用中,由服务器创建的唯一的一个对象是ServletContext 2)ServletContext对象在每一个Servlet中取得都是相 ...
- HTML5学习(六)---------SVG 与Canvas
参考教程:http://www.w3school.com.cn/html5/html_5_canvas_vs_svg.asp Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不 ...
- 谈谈Runtime类中的freeMemory,totalMemory,maxMemory等几个方法
最近在网上看到一些人讨论到java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些问题,很多人感到很疑惑,为什么,在jav ...
- 纯干货,Spring-data-jpa详解,全方位介绍
本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求.这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- 【转】parallels desktop 11 授权许可文件删除方法
原文网址:http://www.macappstore.net/tips/parallels-desktop-uninstall/ 很多同学在安装parallels desktop 11破解版后显示还 ...