c#基础四
写入一个XML文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _03写入一个XML文件
{
class Program
{
static void Main(string[] args)
{
//1、创建一个XML文档对象
XmlDocument doc = new XmlDocument();
//2、创建第一行描述信息
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
//3、将创建的第一行数据添加到文档中
doc.AppendChild(dec);
//4、给文档添加根节点
XmlElement books = doc.CreateElement("Books");
//5、将根节点添加给文档对象
doc.AppendChild(books); //6、给根节点添加子节点
XmlElement book1 = doc.CreateElement("Book");
//将子节点book1添加到根节点下
books.AppendChild(book1); //7、给book1添加子节点
XmlElement bookName1 = doc.CreateElement("BookName");
//8、设置标签中显示的文本
bookName1.InnerText = "水浒传";
book1.AppendChild(bookName1); XmlElement author1 = doc.CreateElement("Author");
author1.InnerText = "<authorName>匿名</authorName>";
book1.AppendChild(author1); XmlElement price1 = doc.CreateElement("Price");
price1.InnerXml = "<authorName>匿名</authorName>";
book1.AppendChild(price1); XmlElement des1 = doc.CreateElement("Des");
des1.InnerXml = "好看,顶!~!!!!";
book1.AppendChild(des1); Console.WriteLine("保存成功");
doc.Save("Book.xml");
Console.ReadKey();
}
}
}
添加带属性的XML文档
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _04添加带属性的XML文档
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec); XmlElement order = doc.CreateElement("Order");
doc.AppendChild(order); XmlElement customerName = doc.CreateElement("CustomerName");
customerName.InnerXml = "刘洋";
order.AppendChild(customerName); XmlElement orderNumber = doc.CreateElement("OrderNumber");
orderNumber.InnerXml = "";
order.AppendChild(orderNumber); XmlElement items = doc.CreateElement("Items");
order.AppendChild(items); XmlElement orderItem1 = doc.CreateElement("OrderItem");
orderItem1.SetAttribute("Name", "码表");
orderItem1.SetAttribute("Count", "");
items.AppendChild(orderItem1); XmlElement orderItem2 = doc.CreateElement("OrderItem");
orderItem2.SetAttribute("Name", "雨衣");
orderItem2.SetAttribute("Count", "");
items.AppendChild(orderItem2); doc.Save("Order.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
}
}
}
文档对象模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _05文档对象模型
{
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>();
list.Add(new Student() { ID = , Name = "yhb", Gender = '男', Age = });
list.Add(new Student() { ID = , Name = "wl", Gender = '女', Age = });
list.Add(new Student() { ID = , Name = "刘德华", Gender = '男', Age = });
list.Add(new Student() { ID = , Name = "张学友", Gender = '男', Age = });
list.Add(new Student() { ID = , Name = "哥哥", Gender = '男', Age = - }); XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); XmlElement person = doc.CreateElement("Person");
doc.AppendChild(person); //通过循环List集合,获得所有对象 以节点的形式添加到XML文档中
for (int i = ; i < list.Count; i++)
{
XmlElement student = doc.CreateElement("Student");
student.SetAttribute("studentID", list[i].ID.ToString());
XmlElement name = doc.CreateElement("Name");
name.InnerXml = list[i].Name;
XmlElement age = doc.CreateElement("Age");
age.InnerXml = list[i].Age.ToString();
XmlElement gender = doc.CreateElement("Gender");
gender.InnerXml = list[i].Gender.ToString();
//添加
person.AppendChild(student);
student.AppendChild(name);
student.AppendChild(age);
student.AppendChild(gender); } doc.Save("Student.xml");
Console.WriteLine("保存成功");
Console.ReadKey(); }
} class Student
{
public int Age { get; set; }
public string Name { get; set; }
public int ID { get; set; }
public char Gender { get; set; }
}
}
对xml文档实现增删改查
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace _06对xml文档实现增删改查
{
class Program
{
static void Main(string[] args)
{
//XMLDocument
#region 对xml文档实现追加的需求
//XmlDocument doc = new XmlDocument();
////首先判断xml文档是否存在 如果存在 则追加 否则创建一个
//if (File.Exists("Student.xml"))
//{
// //加载进来
// doc.Load("Student.xml");
// //追加
// //获得根节点 给根节点添加子节点
// XmlElement person = doc.DocumentElement; // XmlElement student = doc.CreateElement("Student");
// student.SetAttribute("studentID", "10");
// person.AppendChild(student); // XmlElement name = doc.CreateElement("Name");
// name.InnerXml = "我是新来哒";
// student.AppendChild(name); // XmlElement age = doc.CreateElement("Age");
// age.InnerXml = "18";
// student.AppendChild(age); // XmlElement gender = doc.CreateElement("Gender");
// gender.InnerXml = "女";
// student.AppendChild(gender); //}
//else
//{
// //不存在 // XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
// doc.AppendChild(dec); // XmlElement person = doc.CreateElement("Person");
// doc.AppendChild(person); // XmlElement student = doc.CreateElement("Student");
// student.SetAttribute("studentID", "110");
// person.AppendChild(student); // XmlElement name = doc.CreateElement("Name");
// name.InnerXml = "张三三李思思";
// student.AppendChild(name); // XmlElement age = doc.CreateElement("Age");
// age.InnerXml = "28";
// student.AppendChild(age); // XmlElement gender = doc.CreateElement("Gender");
// gender.InnerXml = "男";
// student.AppendChild(gender);
//} //doc.Save("Student.xml");
//Console.WriteLine("保存成功");
#endregion #region 读取XML文档
//XmlDocument doc = new XmlDocument(); //doc.Load("OrDER.xml"); ////还是 先获得根节点
//XmlElement order = doc.DocumentElement;
////获得根节点下面的所有子节点
//XmlNodeList xnl = order.ChildNodes; //foreach (XmlNode item in xnl)
//{
// Console.WriteLine(item.InnerText);
//}
//XmlElement items = order["Items"];
//XmlNodeList xnl2 = items.ChildNodes;
//foreach (XmlNode item in xnl2)
//{
// Console.WriteLine(item.Attributes["Name"].Value);
// Console.WriteLine(item.Attributes["Count"].Value); // if (item.Attributes["Name"].Value == "手套")
// {
// item.Attributes["Count"].Value = "新来哒";
// }
//} //doc.Save("OrDER.xml");
#endregion #region 使用XPath的方式来读取XML文件 //XmlDocument doc = new XmlDocument();
//doc.Load("order.xml");
////获得根节点
//XmlElement order = doc.DocumentElement;
//XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']"); ////Console.WriteLine(xn.Attributes["Name"].Value); //xn.Attributes["Count"].Value = "woshi new"; //doc.Save("order.xml");
//Console.WriteLine("保存成功");
#endregion XmlDocument doc = new XmlDocument(); doc.Load("order.xml"); //doc.RemoveAll();不行 根节点不允许删除 XmlElement order = doc.DocumentElement; //order.RemoveAll();移除根节点下的所有子节点 // XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']"); // //让orderItem去删除属性
// XmlNode orderItem = order.SelectSingleNode("/Order/Items/OrderItem"); // //获得Items节点 // //XmlNode items = order["Items"];//order.SelectSingleNode("/Order/Items"); //// items.RemoveChild(xn);移除当前节点 // //orderItem.RemoveAttributeNode(xn.Attributes["Count"]); // xn.Attributes.RemoveNamedItem("Count"); // doc.Save("order.xml");
// Console.WriteLine("删除成功");
// Console.ReadKey(); //XPath
}
}
}
c#基础四的更多相关文章
- Python全栈开发【基础四】
Python全栈开发[基础四] 本节内容: 匿名函数(lambda) 函数式编程(map,filter,reduce) 文件处理 迭代器 三元表达式 列表解析与生成器表达式 生成器 匿名函数 lamb ...
- Bootstrap<基础四> 代码
Bootstrap 允许您以两种方式显示代码: 第一种是 <code> 标签.如果您想要内联显示代码,那么您应该使用 <code> 标签. 第二种是 <pre> 标 ...
- Python 基础 四 面向对象杂谈
Python 基础 四 面向对象杂谈 一.isinstance(obj,cls) 与issubcalss(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls ...
- C#_02.13_基础四_.NET方法
C#_02.13_基础四_.NET方法 一.方法概述: 方法是一块具有名称的代码.可以通过方法进行调用而在别的地方执行,也可以把数据传入方法并接受数据输出. 二.方法的结构: 方法头 AND 方法 ...
- day 68 Django基础四之模板系统
Django基础四之模板系统 本节目录 一 语法 二 变量 三 过滤器 四 标签Tags 五 模板继承 六 组件 七 自定义标签和过滤器 八 静态文件相关 一 语法 模板渲染的官方文档 关 ...
- day 54 Django基础四之模板系统
Django基础四之模板系统 本节目录 一 语法 二 变量 三 过滤器 四 标签Tags 五 模板继承 六 组件 七 自定义标签和过滤器 八 静态文件相关 一 语法 模板渲染的官方文档 关于模 ...
- Django基础四之测试环境和ORM查询
Django基础四之测试环境和ORM查询 目录 Django基础四之测试环境和ORM查询 1. 搭建测试环境 1.1 测试环境搭建方法: 1.2 使用测试环境对数据库进行CURD 1.3 返回Quer ...
- django-rest-framework 基础四 过滤、排序、分页、异常处理
django-rest-framework 基础四 过滤.排序.分页.异常处理 目录 django-rest-framework 基础四 过滤.排序.分页.异常处理 1. 过滤 1.1 内置过滤类 1 ...
- python学习笔记(基础四:模块初识、pyc和PyCodeObject是什么)
一.模块初识(一) 模块,也叫库.库有标准库第三方库. 注意事项:文件名不能和导入的模块名相同 1. sys模块 import sys print(sys.path) #打印环境变量 print(sy ...
- node.js(基础四)_express基础
一.前言 本次内容主要包括: 1.express的基本用法 2.express中的静 ...
随机推荐
- python 教程 第十五章、 结构布局
第十五章. 结构布局 #!/usr/bin/env python #(1)起始行 "this is a module" #(2)模块文档 import sys #(3)模块导入 d ...
- SQL like使用 模糊查询
模糊查询: 参考资料:http://www.w3school.com.cn/sql/sql_wildcards.asp 在搜索数据库中的数据时,您能够使用 SQL 通配符. SQL 通配符 Like ...
- Angular使用echarts
安装 npm install echarts --save npm install @types/echarts --save 基本使用 定义一个dom <div id="chart& ...
- [LeetCode OJ] Copy List with Random Pointer 扩大
职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意:对一个有回路的链表的深复制 解题:这道题我AC了之后才发 ...
- QProcess::startDetached(5.10有了一种新的方式)
From Qt 5.10 on, there is a new way how to start detached processes with QProcess. Of course you kno ...
- WP8.1使用HttpClient类
Uri uri = new Uri("http://www.cnsos.net/weburl/index.htm", UriKind.Absolute); HttpClient m ...
- 在Azure中搭建Ghost博客并绑定自定义域名和HTTPS
绪论 之前一直使用cnblog写博客,现在将博客迁移至Microsoft Azure上的Ghost博客上,Ghost博客使用Markdown书写博客,页面简洁,是我喜欢的风格.具体参见官网:https ...
- CREATE CSS3是一款在线集成众多CSS3功能的生成器,可以在线生成常用的CSS3效果
CREATE CSS3是一款在线集成众多CSS3功能的生成器,可以在线生成常用的CSS3效果 CREATE CSS3 彩蛋爆料直击现场 CREATE CSS3是一款在线集成众多CSS3功能的生成器,可 ...
- QT 文件拖放事件dropEvent和dragEnterEvent
重载以下两个函数,可以实现将文本文件拖放进文本编辑器 void MainWindow::dragEnterEvent(QDragEnterEvent *event)//拖进事件 { if(event- ...
- 《C++ Primer》读书笔记 第一章
读<C++ Primer>才知道,自己对C++知之甚少... 写个博客记录下自己C++的成长,只是读书笔记,不是对<C++ Primer>知识点的总结,而是对自己在书上看到的以 ...