传统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左右吧~平时也没人来闲聊几句,刷空间暑假也没啥动态,听音乐吧...~有些确实不好听,于是就不得不打断手头的工作去点击下 ...
随机推荐
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
启动 Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误的解决方法 http://blog.csdn.net/z ...
- netty 实现socket服务端编写
import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap; import io.netty.channe ...
- Flash Media Server 5.0 (FMS)注册码
flash media server 4.5 及最新 flash media server 5.0 注册码 防止图片打不开时: Name:tam/CORE Serial:1652-5580-8001- ...
- 【HDOJ】4328 Cut the cake
将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. /* 4328 */ #include <iostream> #include <sstream&g ...
- JS将下拉框的disable变为able的方法
在jquery中可以通过jqueryObj.attr("disabled","disabled")将页面中某个元素置为不可编辑或触发状态,但是在jquery的A ...
- POJ3352Road Construction(无向图强连通)
http://poj.org/problem?id=3352 无向图强连通分量缩点 知道一个等式: 若要使得任意一棵树,在增加若干条边后,变成一个双连通图,那么 至少增加的边数 =( 这棵树总度数为1 ...
- 转: 解决MSYS2下的中文乱码问题
解决方案 新建/usr/bin/win: 12 #!/bin/bash$@ |iconv -f gbk -t utf-8 新建/etc/profile.d/alias.sh: 12345678 ali ...
- Flask
#environ:一个包含所有HTTP请求信息的dict对象 #start_response:一个发送HTTP响应的函数 def application(environ, start_response ...
- YQL
YQL,(Yahoo! Query Language)是一种支持对互联网上的数据进行查询.过滤.连接.类似SQL语法的简单语言.用YQL官方的话:有了YQL,开发人员只需要使用一种 简单的查询语言即可 ...
- Android 隐藏RadoiButton左边按钮
声明方式 添加属性 android:button=“@null”? 代码方式 radioBtn.setButtonDrawable(new StateListDrawable());