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中的静 ...
随机推荐
- cross-compile-openssl-windows.sh,cross-compile-curl-windows.sh,cross-compile-zlib-windows.sh,build-zlib-visual-studio-2015-cli.bat
https://gist.github.com/artynet build zlib with Visual Studio CLI toolhttps://gist.github.com/artyne ...
- Linux 下编译并安装配置 Qt 4.53全过程
最近准备做 Nokia 的 Symbian,Maemo 下触摸屏开发.考虑到程序的跨平台可移植性,最终选择使用 Qt 开发.相对来说,国内关于 Qt 相关文档并不算很多.作者将 Linux 下编译并安 ...
- Python第一个基本教程4章 词典: 当指数不工作时也
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyri ...
- WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮
原文:WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮 在边框中加入一些元素,在应用程序的界面设计中,已经开始流行起来.特别是在浏览器(Crome,IE,Firefox,Opera)中都有应用. ...
- 浅谈 Swift 中的 Optionals
input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...
- WPF后台生成datatemplate(TreeViewItem例子)
public void loadCheckListDataTemplate(TreeViewItem tvi) { DataTemplate cdt = new DataTemplate(); Fra ...
- Android项目实战(四十四):Zxing二维码切换横屏扫描
原文:Android项目实战(四十四):Zxing二维码切换横屏扫描 Demo链接 默认是竖屏扫描,但是当我们在清单文件中配置横屏显示的时候: <activity android:name=&q ...
- Android零基础入门第83节:Activity间数据传递方法汇总
在Activity间传递的数据一般比较简单,但是有时候实际开发中也会传一些比较复杂的数据,本节一起来学习更多Activity间数据的传递. 一.常用数据类型 在前面几节我们只学习了一些常用类型的数据传 ...
- ML:吴恩达 机器学习 课程笔记(Week1~2)
吴恩达(Andrew Ng)机器学习课程:课程主页 由于博客编辑器有些不顺手,所有的课程笔记将全部以手写照片形式上传.有机会将在之后上传课程中各个ML算法实现的Octave版本. Linear Reg ...
- wchar_t string on Linux, OS X and Windows
Making wchar_t work on Linux, OS X and Windows for CMarkup release 10.1 I learned a couple of humble ...