XmlDocument
XmlDocument增删改查。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml; namespace _02XmlDocumentReview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//xml文件储存路径
private static string xmlFilePath = Application.StartupPath + "\\ListComputer.xml";
//创建
private void button1_Click(object sender, EventArgs e)
{
GenerateXml(xmlFilePath);
MessageBox.Show("执行成功!");
} private void GenerateXml(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDec); XmlElement root = xmlDoc.CreateElement("computers");
xmlDoc.AppendChild(root); XmlElement firstElement1 = xmlDoc.CreateElement("computer");
firstElement1.SetAttribute("ID", "11111111");
firstElement1.SetAttribute("Description", "Made In China");
root.AppendChild(firstElement1); XmlElement secondElement11 = xmlDoc.CreateElement("Name");
secondElement11.InnerText = "Lenovo";
firstElement1.AppendChild(secondElement11); XmlElement secondElement12 = xmlDoc.CreateElement("Price");
secondElement12.InnerText = "5000";
firstElement1.AppendChild(secondElement12); XmlElement firstElement2 = xmlDoc.CreateElement("computer");
firstElement2.SetAttribute("ID", "22222222");
firstElement2.SetAttribute("Description", "Made In USA");
root.AppendChild(firstElement2); XmlElement secondElement21 = xmlDoc.CreateElement("Name");
secondElement21.InnerText = "IBM";
firstElement2.AppendChild(secondElement21); XmlElement secondElement22 = xmlDoc.CreateElement("Price");
secondElement22.InnerText = "10000";
firstElement2.AppendChild(secondElement22); xmlDoc.Save(xmlFilePath); }
//遍历
private void button2_Click(object sender, EventArgs e)
{
GetXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
} private void GetXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
//方法1:
XmlNode root = xmlDoc.DocumentElement;
//方法2:
XmlNode rootElement = xmlDoc.SelectSingleNode("computers");
foreach (XmlNode node in rootElement.ChildNodes)//root.ChildNodes)
{
string name = string.Empty;
string value = string.Empty;
//节点属性
foreach (XmlAttribute attri in node.Attributes)
{
name = attri.Name;
value = attri.Value;
MessageBox.Show(string.Format("{0}={1}", name, value));
} if (node.HasChildNodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
name = node.ChildNodes[i].Name;
//value = node.ChildNodes[i].Value; //错
value = node.ChildNodes[i].InnerText;
MessageBox.Show(string.Format("{0}={1}", name, value));
}
}
}
}
//修改
private void button3_Click(object sender, EventArgs e)
{
UpdateXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
} private void UpdateXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNode root = xmlDoc.DocumentElement; foreach (XmlNode node in root.ChildNodes)
{
if (node.Attributes["Description"].Value.Equals("Made In USA"))
{
node.Attributes["Description"].Value = "Made In HK";
}
if (node.HasChildNodes && node.Attributes["ID"].Value == "22222222")
{
node.ChildNodes[1].InnerText = "8000";
}
}
xmlDoc.Save(xmlFilePath);
}
//新增
private void button4_Click(object sender, EventArgs e)
{
AddXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
} private void AddXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNode root = xmlDoc.DocumentElement; foreach (XmlNode node in root.ChildNodes)
{
XmlElement newElement = xmlDoc.CreateElement("Color");
newElement.SetAttribute("IsMixed", "Yes");
newElement.InnerText = "Black";
//xmlDoc.AppendChild(newElement);//错误
//root.AppendChild(newElement);//错误
//节点的级别要清晰
node.AppendChild(newElement);
}
xmlDoc.Save(xmlFilePath);
}
//删除
private void button5_Click(object sender, EventArgs e)
{
DeleteXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
} private void DeleteXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNode root = xmlDoc.DocumentElement; foreach (XmlNode node in root.ChildNodes)
{
XmlNode lastNode = node.LastChild;
lastNode.RemoveAll();
node.RemoveChild(lastNode);
}
xmlDoc.Save(xmlFilePath);
}
}
}
XmlDocument的更多相关文章
- 用ORM的思想操作XML文档,一个对象就搞定不要太简单。滚蛋吧!XmlDocument、XmlNode、Xml***……
大家有没有这样的感受,一涉及XML文档操作就得百度一遍.是不是非!常!烦!.各种类型,各种方法,更别提为了找到一个节点多费劲.本来想写个XML操作的工具方法,写了两行一想既然XML文档是有规律的,如果 ...
- C# XMLDocument
今天开发一个WPF模块需要本地化保存一些用户设置,鉴于数据量不大,用XML. (要是再小的话可以用Resources 和 Settings). 清晰简短教程移步:http://bdk82924.ite ...
- XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate
namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...
- C# XML技术总结之XDocument 和XmlDocument
引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)
主界面
- jquery中ajax在firefox浏览器下“object XMLDocument”返回结果的解决办法
asp.net中借助jquery的ajax处理功能,使用起来很方便.但是在firefox下获得的data报错object XMLDocument.这是因为默认的情况下把datatype用html来解析 ...
- XmlDocument解析Soap格式文件案例:
private static string Analysis(string strResult) { var doc = new System.Xml.XmlDocument(); //加载soap文 ...
- 将XmlDocument转换成XDocument
XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...
- XmlDocument To String
一.从String xml到XmlDocument的: string xml = "<XML><Test>Hello World</Test></X ...
- c# xml的增删改查操作 xmlDocument 的用法
1.将xml转换为DataTable string path = "";//xml的位置StringReader sr = null;XmlTextReader xmlReader ...
随机推荐
- js将月份转换为英文简写的形式
本文通过代码实例介绍一下如何实现将月份转换为英文简写的形式. 比如将一月份转换为Jan,需要的朋友可以做一下参考. 代码实例如下: [JavaScript] 纯文本查看 复制代码运行代码 1 2 3 ...
- js 二维码
https://larsjung.de/jquery-qrcode/ 源码 <!DOCTYPE html> <html> <head> <title>j ...
- STL_算法_最小值和最大值(min_element、max_element)
C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) min_element.max_element 找最小.最大值. 非常easy没什么大作用 #include<iost ...
- UVA 437 The Tower of Babylon巴比伦塔
题意:有n(n≤30)种立方体,每种有无穷多个.要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽. 评测地址:http:/ ...
- Linux上ln命令详细说明及软链接和硬链接的区别
硬链接(hard link) UNIX文件系统提供了一种将不同文件链接至同一个文件的机制,我们称这种机制为链接.它可以使得单个程序对同一文件使用不同的名字.这样的好处是文件系 统只存在一个文件的副本, ...
- 汉诺塔算法c++源代码(递归与非递归)[转]
算法介绍: 其实算法非常简单,当盘子的个数为n时,移动的次数应等于2^n - 1(有兴趣的可以自己证明试试看).后来一位美国学者发现一种出人意料的简单方法,只要轮流进行两步操作就可以了.首先把三根柱 ...
- cocos2dx笔记1:概述
1.核心的类和功能 CCDirector gameLoop,实现场景绘制.多个场景之间切换控制.控制游戏的停止,暂停,等生命周期. CCScene 场景类,每一个场景能够理解为一个游戏镜头.状态 CC ...
- socket.io实现在线群聊
我自己在用socket.io开发,对官方网站上的文档,进行简单的整理,然后自己写了一个简单的聊天程序.最最开始 先安装socket.io: npm install socket.io 利用Node的搭 ...
- bootstrap3.0学习笔记记录1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- lmbench andlmbench 移植测试
/*********************************************************************** * lmbench andlmbench 移植测试 * ...