using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
namespace ConsoleApplication1
{
/// <summary>
/// XML操作类
/// </summary>
public class Program
{
/// <summary>
/// 入口方法
/// </summary>
/// <param name="args">参数列表</param>
public static void Main(string[] args)
{
string filePath="cars.xml";
CreateXmlFile(filePath);
ReadXmlFile(filePath);
ModifyXmlFile(filePath);
DeleteXmlNode(filePath);
}
/// <summary>
/// 创建一个XML文件
/// </summary>
/// <param name="filePath">文件路径</param>
public static void CreateXmlFile(string filePath)
{
// 01.声明XMLDocument
XmlDocument doc = new XmlDocument();
// 02.向其中加入头部声明
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", "");
doc.AppendChild(declaration);
// 03.向其中加入子节点
XmlElement root = doc.CreateElement("cars");
root.SetAttribute("id", "10001");// 向结点中添加属性
// 添加奔驰汽车信息
XmlElement benz = doc.CreateElement("benz");
XmlElement benzName = doc.CreateElement("Name");
benzName.InnerText = "奔驰";//InnerText
benz.AppendChild(benzName);
XmlElement benzMaxSpeed = doc.CreateElement("MaxSpeed");
benzMaxSpeed.InnerText = "300";
benz.AppendChild(benzMaxSpeed);
root.AppendChild(benz);
// 添加宝马汽车信息
XmlElement bmw = doc.CreateElement("bmw");
bmw.InnerXml = "<Name>宝马</Name><MaxSpeed>299</MaxSpeed>";//InnerXml
root.AppendChild(bmw);
// 04.保存该文件
doc.AppendChild(root);
doc.Save(filePath);
}
/// <summary>
/// 读取Xml文件
/// </summary>
/// <param name="filePath">文件路径</param>
public static void ReadXmlFile(string filePath)
{
// 第一种方法:使用XmlDocument.Load()
if (File.Exists(filePath))
{
// 01.载入XML文档
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
// 02.读取根节点及子节点
XmlNode root = doc.SelectSingleNode("cars");
if (root.HasChildNodes)
{
XmlNodeList xnd = root.ChildNodes;
foreach (XmlNode xn in xnd)
{
XmlElement xe = (XmlElement)xn;
Console.WriteLine(xn.InnerXml);
}
}
}
// 第二种方法:使用XmlDocument.LoadXml()
if (File.Exists(filePath))
{
using (StreamReader sr = new StreamReader(filePath))
{
string xmlStr = sr.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
// 以下代码和上面一样
}
}
// Load(),与LoadXml()的区别是:
// 前者载入XML文档
// 后者载入XML字符串
}
/// <summary>
/// 修改Xml文档信息
/// </summary>
/// <param name="filePath">文档路径</param>
public static void ModifyXmlFile(string filePath)
{
if (File.Exists(filePath))
{
// 01.载入Xml文档
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlElement root = (XmlElement)doc.SelectSingleNode("cars");
// 02.修改属性信息
root.SetAttribute("id","998");
// 03.修改结点信息
XmlNode bwnMaxSpeed = (XmlElement)doc.SelectSingleNode("cars/bmw/MaxSpeed");
bwnMaxSpeed.InnerText = "319";
// 04.保存文档
doc.Save("mcar.xml");
}
}
/// <summary>
/// 删除结点或属性信息
/// </summary>
/// <param name="filePath">Xml文档路径</param>
public static void DeleteXmlNode(string filePath)
{
if (File.Exists(filePath))
{
// 01.载入文档
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlElement root = (XmlElement)doc.SelectSingleNode("cars");
// 02.删除属性信息
if (root.HasAttribute("id"))
{
root.RemoveAttribute("id");
}
// 03.删除结点信息
XmlNodeList xnd = root.ChildNodes;
foreach (XmlNode xn in xnd)
{
XmlNode subXn = xn.FirstChild;
if (subXn.InnerText == "宝马")
{
root.RemoveChild(xn);
}
}
// 04.保存更改
doc.Save("dcar.xml");
}
}
}
}