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的更多相关文章

  1. 用ORM的思想操作XML文档,一个对象就搞定不要太简单。滚蛋吧!XmlDocument、XmlNode、Xml***……

    大家有没有这样的感受,一涉及XML文档操作就得百度一遍.是不是非!常!烦!.各种类型,各种方法,更别提为了找到一个节点多费劲.本来想写个XML操作的工具方法,写了两行一想既然XML文档是有规律的,如果 ...

  2. C# XMLDocument

    今天开发一个WPF模块需要本地化保存一些用户设置,鉴于数据量不大,用XML. (要是再小的话可以用Resources 和 Settings). 清晰简短教程移步:http://bdk82924.ite ...

  3. XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate

    namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...

  4. C# XML技术总结之XDocument 和XmlDocument

    引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...

  5. c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)

    主界面

  6. jquery中ajax在firefox浏览器下“object XMLDocument”返回结果的解决办法

    asp.net中借助jquery的ajax处理功能,使用起来很方便.但是在firefox下获得的data报错object XMLDocument.这是因为默认的情况下把datatype用html来解析 ...

  7. XmlDocument解析Soap格式文件案例:

    private static string Analysis(string strResult) { var doc = new System.Xml.XmlDocument(); //加载soap文 ...

  8. 将XmlDocument转换成XDocument

    XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...

  9. XmlDocument To String

    一.从String xml到XmlDocument的: string xml = "<XML><Test>Hello World</Test></X ...

  10. c# xml的增删改查操作 xmlDocument 的用法

    1.将xml转换为DataTable string path = "";//xml的位置StringReader sr = null;XmlTextReader xmlReader ...

随机推荐

  1. InfoQ中文站特供稿件:Rust编程语言的核心部件

    本文为InfoQ中文站特供稿件.首发地址为: http://www.infoq.com/cn/articles/rust-core-components .如需转载.请与InfoQ中文站联系. 原文发 ...

  2. GCC编译动态和静态链接库例子

    我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而是 ...

  3. 【iOS系列】-UINavigationController的使用(Segue传递数据)

    [iOS系列]-UINavigationController的使用 UINavigationController是以以栈(先进后出)的形式保存子控制器, 常用属性: UINavigationItem有 ...

  4. 堆排序C++实现

    //heap sort //堆排序能够分为两个过程.其一是建堆.其二是出堆 //堆是一种全然二叉树,所以它能够用数组进行存储. //堆可分为最大堆和最小堆.最大堆指任一节点的值都大于其左右孩子节点的值 ...

  5. mysql备份及还原

    mysql怎样复制数据库?或者说,将数据库拷贝到另外一台机? 按照我的理解,复制数据库,如果是: 1.直接拷贝数据库文件,应该先停数据服务,否则拷不出来.但是,生产机器,哪能说停就停呢? 2.在线拷贝 ...

  6. XMU 1050 Diffuse Secret 【最短路】

    1050: Diffuse Secret Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 10  Solved: 8[Submit][Status][We ...

  7. mondb08---导入导出

    //Mongodb数据的导入导出 : 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的. 所以,都有如下通用选项:(本地机就不用这个了) -h host 主机 --port port ...

  8. 网络驱动移植之例解netdev_priv函数

    版权声明:本文为博主原创文章,未经博主允许不得转载. 开发平台:Ubuntu 11.04 编译器:gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4) 内核 ...

  9. 高清摄像头MIPI CSI2接口浅解【转】

    本文转载自:http://blog.csdn.net/u012075739/article/details/44672435 MIPI摄像头常见于手机.平板中,支持500万像素以上高清分辨率.它的全称 ...

  10. html5--js函数在canvas中的应用

    html5--js函数在canvas中的应用 总结: 1.script中的函数写了要调用 2.rgb()这样的模式的色彩比较适合做变量 3.body的onload事件 4.带参函数 效果: 代码: & ...