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. LuaInterface简单介绍

    LuaInterface简单介绍 Lua是一种非常好的扩展性语言.Lua解释器被设计成一个非常easy嵌入到宿主程序的库.LuaInterface则用于实现Lua和CLR的混合编程. (一)Lua f ...

  2. 全局最小割模版 n^3

    //点标从0-n-1, 開始时先init 复杂度n^3 //对于边(u,v,flow): //g[u][v]+=flow; //g[v][u]+=flow; typedef long long ll; ...

  3. 记一次Tomcat无法正常启动的查错与解决之路

    使用LombozEclipse运行某Web应用,结果总是404. 换另一个Eclipse运行,还是404. 换Tomcat到更高版本,还是404. 直接启动Tomcat,闪退. 用重定向拦截输出,可惜 ...

  4. web编程非常实用的在线工具大全

    目前,不管是前端开发人员还是个人站长,经常需要一些代码处理类的工具,比如:代码对比.代码格式化.图标制作等.有时就是一时急用可电脑上又没有安装相关的软件,这里为大家收集了一些我们经常会用到的在线工具. ...

  5. Fix "Unable to lock the administration directory (/var/lib/dpkg/)" in Ubuntu

    While using the apt-get command or the relatively new APT package management tool in Ubuntu Linux or ...

  6. Thinking in Java -- 类型信息RTTI

    Thinking in Java – 类型信息 个人感觉 java 中的比較难的部分了,在看了些netty源代码发现事实上这块很实用. 这章重点是RTTI和反射.先说下自己的理解 RTTI是执行时识别 ...

  7. linkerd——针对java的为微服务提供可靠性的proxy,服务发现重试LB等

    Buoyant是一家云服务公司,宣布了Linkerd(发音为“linker-DEE”)的一周年纪念日,这是一个基于微服务的原生云应用程序的开源“服务网格”项目.诚如公告所述: 在20世纪90年代,TC ...

  8. FMDB数据库队列

    一.代码示例 1.需要先导入FMDB框架和头文件,由于该框架依赖于libsqlite库,所以还应该导入该库. 2.代码如下: 1 // 2 // YYViewController.m 3 // 05- ...

  9. bzoj 5329 战略游戏

    题目大意: 一个无向图 Q个询问 每次给一些点的集合 求有多少个点满足去掉这个点后使这些点的集合中有一个点对不连通 思路: 点双缩点 相当于每次求这些点中的所有路径上的圆点个数 可以将这些点按dfs序 ...

  10. BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包

    BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...