C#|C#+ XML 增删改查
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
namespace MvcApplication_Test
{
public class CreateXmlFile
{
///
///创建xml文件
///
public void CreateXml()
{
XmlDocument xmlDoc = new XmlDocument();
//创建类型声明节点
XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmlDoc.AppendChild(node);
//创建根节点
XmlNode root = xmlDoc.CreateElement("Users");
xmlDoc.AppendChild(root);
XmlElement person = xmlDoc.CreateElement("Person");
person.SetAttribute("id", "1");
//创建节点为Person,id=1
person.AppendChild(CreateNode(xmlDoc, person, "name", "张三"));
person.AppendChild(CreateNode(xmlDoc, person,"Age", "24"));
root.AppendChild(person);
//XmlNode node2 = xmlDoc.CreateNode(XmlNodeType.Element, "Person", null);
//CreateNode(xmlDoc, node2, "name", "xiaolai");
//CreateNode(xmlDoc, node2, "sex", "female");
//CreateNode(xmlDoc, node2, "age", "23");
//root.AppendChild(node2);
try
{
string url = AppDomain.CurrentDomain.BaseDirectory + "xml\\UserData.xml";
xmlDoc.Save(url);
}
catch (Exception e)
{
//显示错误信息
Console.WriteLine(e.Message);
}
//Console.ReadLine();
}
/// 创建节点
///
/// 【C#|C#+ XML 增删改查】xml文档
/// 父节点
/// 节点名
/// 节点值
///
public XmlNode CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
{
XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
node.InnerText = value;
//parentNode.AppendChild(node);
return node;
}
///
/// 加载解析xml文件
///
public void ReadXml()
{
try
{
XmlDocument xmlDoc = LoadXml();
XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;
XmlElement root = xmlDoc.DocumentElement;
//获取根节点
XmlNodeList user = root.GetElementsByTagName("Person");
//获取user子节点集合foreach (XmlElement node in topM)
{
string id = ((XmlElement)node).GetAttribute("id");
//获取Name属性值
string name = ((XmlElement)node).GetElementsByTagName("Name")[0].InnerText;
//获取Age子XmlElement集合
string age = ((XmlElement)node).GetElementsByTagName("Age")[0].InnerText;
}}
catch (Exception e)
{
CreateXml();
}}
public void EditXml()
{
try
{
string url = AppDomain.CurrentDomain.BaseDirectory + "xml\\UserData.xml";
XmlDocument xmlDoc = LoadXml();
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList personNodes = root.GetElementsByTagName("Person");
foreach (XmlNode node in personNodes)
{
XmlElement ele = (XmlElement)node;
if (ele.GetAttribute("id") == "1")
{
XmlElement nameEle = (XmlElement)ele.GetElementsByTagName("name")[0];
nameEle.InnerText = nameEle.InnerText + "修改";
}
}
xmlDoc.Save(url);
}
catch (Exception e)
{
}
}
///
///删除独立的节点
///
public void DelectXmlnode()
{
try
{
string url = AppDomain.CurrentDomain.BaseDirectory + "xml\\UserData.xml";
XmlDocument xmlDoc = LoadXml();
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList personNodes = root.GetElementsByTagName("Person");
//获取Person子节点集合
XmlNode selectNode = root.SelectSingleNode(("/Users/Person[@id='3']"));
if (selectNode != null)
{
root.RemoveChild(selectNode);
xmlDoc.Save(url);
}}
catch (Exception e)
{
}
}
private XmlDocument LoadXml()
{
string url = AppDomain.CurrentDomain.BaseDirectory + "xml\\UserData.xml";
XmlDocument doc = new XmlDocument();
doc.Load(url);
return doc;
}
///
/////根节点的添加独立子节点
///
public void AddNode()
{
string url = AppDomain.CurrentDomain.BaseDirectory + "xml\\UserData.xml";
XmlDocument xmlDoc = LoadXml();
XmlElement root = xmlDoc.DocumentElement;
XmlElement person = xmlDoc.CreateElement("Person");
person.SetAttribute("id", "3");
person.AppendChild(CreateNode(xmlDoc,person ,"name", "Elephant"));
person.AppendChild(CreateNode(xmlDoc,person, "Age", "23"));
xmlDoc.DocumentElement.AppendChild(person);
//xml有且只能有一个根节点 把doc.AppendChild(Element);
改成doc.DocumentElement.AppendChild(Element)
//xmlDoc.AppendChild(person);
xmlDoc.Save(url);
}
}
}
文章图片
推荐阅读
- 增长黑客的海盗法则
- mybatisplus如何在xml的连表查询中使用queryWrapper
- Excel|Excel 2013 新增功能之瞬间填充整列数据!
- 微信小程序基础知识
- 木村拓哉透露“增高术”,16岁女儿身高1米7,每天都坚持一件事
- JavaScript|JavaScript: BOM对象 和 DOM 对象的增删改查
- JavaScript|JavaScript之DOM增删改查(重点)
- 窝在家里,你闷坏了吗(了解这12个假设,给自己的心理增加免疫力)
- MX19022-王阿呆-第4节课作业(做市场行情精准判定与竞品分析#萌新运营苑裂变增长操盘手#)
- 应为自己增值