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 ...
随机推荐
- powerdesign导出SQL时自己主动生成凝视
1.使用脚本的方式 在里面执行 Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' ...
- PHP记录商品历史纪录
/* 记录浏览历史 */ if (!empty($_COOKIE['history'])) { if(stripos($_COOKIE['history'].',',$goods_id.',')=== ...
- JSONObjectWithData方法里options參数选择解释
NSJSONReadingMutableContainers Specifies that arrays and dictionaries are created as mutable object ...
- cocos2d-x中锚点设置及定位方式
问题 在cocos2d演示样例代码HelloCpp中,为什么要将CCMenu设置位置到CCPointZero,即使CCMenu的锚点是在(0.5, 0.5)? 回答 这是由于CCMenu没有使用锚点进 ...
- Bash Shell 解析路径获取文件名称和文件夹名
前言 还是今天再写一个自己主动化打包脚本.用到了从路径名中获取最后的文件名称.这里记录一下实现过程. 当然,最后我也会给出官方的做法.(ps:非常囧,实现完了才发现原来Bash Shell有现成的函数 ...
- 基于Cocos2dx + box2d 实现的愤慨的小鸟Demo
1. Demo初始界面 2. 游戏界面 3. 精确碰撞检測 4. 下载 压缩文件文件夹 AngryBird source 愤慨的小鸟Demo源码,基于Cocos2dx C++,以及box2d技 ...
- 【bzoj2809】[Apio2012]dispatching (左偏树)
我们需要枚举根,然后从其子树内选尽量多的点,薪水不超过M,可是暴力复杂度不对.于是考虑自下而上合并树(开始每棵树内只有一个节点,就是自己) 每个树是一个堆,我们维护树的节点个数和薪水总和,合并时,不断 ...
- C项目实践--图书管理系统(2)
前面在<<C项目实践-图书管理系统(1)>>中把系统中的三大功能模块中可能涉及到的常量,结构体及相关函数进行了声明定义,下来就来实现它们. 执行系统首先从登录到系统开始,所以首 ...
- Poisson distribution 泊松分布 指数分布
Poisson distribution - Wikipedia https://en.wikipedia.org/wiki/Poisson_distribution Jupyter Notebook ...
- 并不对劲的bzoj2820:p2257:YY的GCD
题目大意 \(t\)(\(t\leq10^4\))组数据,给定\(n,m\)(\(n,m\leq10^6\))求 \[\sum_{x=1}^{n}\sum_{y=1}^{m}[gcd(x,y)=1]\ ...