写入一个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
}
}
}

最新文章

  1. Java实验三
  2. empty isset array_key_exists 的区别
  3. 【读书笔记】iOS-GCD-Dispatch Source
  4. 卸载Windows服务
  5. thinkphp文章列表及删除文章
  6. Windows Server 2012 四个版本对比
  7. 从JAVA多线程理解到集群分布式和网络设计的浅析
  8. spoj cot: Count on a tree 主席树
  9. EBS查找运行请求时间,参数等
  10. .net通用防SQL注入漏洞程序(Global.asax方式)
  11. H - Pair: normal and paranormal URAL - 2019
  12. SpringMVC处理ajax请求的注意事项
  13. mysql执行update报错 Err] 1055 - &#39;information_schema.PROFILING.SEQ&#39; isn&#39;t in GROUP BY
  14. Oracle 19c使用dbca来搭建物理DG
  15. web项目访问地址前添加小图片
  16. UOJ#192. 【UR #14】最强跳蚤
  17. C++调用ocx
  18. ubuntu 12.04下gedit查看txt中文乱码解决办法
  19. nexus的pom配置
  20. HTML随笔3

热门文章

  1. c#扩展方法简单
  2. 关于禅道提示未安装VC++环境的问题(做个记录)
  3. 精装友好联络算法实现借壳和RI
  4. 线性渐变、辐射渐变、角度渐变-QLinearGradient,QRadialGradient,QConicalGradient
  5. x名称空间中的内容
  6. Win8 Metro(C#)数字图像处理--2.62图像对数增强
  7. java中==和equels的区别
  8. WPF获取和设置应用程序范围的资源
  9. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区
  10. PHP MYSQL 获取记录总数