已知有一个XML文件(bookshop.xml)如下:

<?xml version="1.0" encoding="gb2312" ?>
<bookshop>
<book genre="fantasy" ISBN="2-2312-2">
<title>Oberon Legacy</title>
<author>Eva</author>
<price>56.5</price>
</book>
</bookshop>

下面看看是怎样读写xml文件的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml; namespace ConsoleApplication1
{
class Program
{
XmlDocument xmlDoc;
///<summary>
/// 插入节点
///</summary>
publicvoid InsertNode()
{
xmlDoc =new XmlDocument();
xmlDoc.Load("bookshop.xml"); //加载xml文件 /*从指定的字符创加载xml文件 例如:
xmlDoc.LoadXml("(<Book bookID='B001'><BookName>jeff</BookName><price>45.6</price></Book>)");
*/
XmlNode root = xmlDoc.SelectSingleNode("bookshop");//查找﹤bookstore﹥
XmlElement xe1 = xmlDoc.CreateElement("book");//创建一个﹤book﹥节点
xe1.SetAttribute("genre", "Sky_Kwolf");//设置该节点genre属性
xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性 XmlElement xesub1 = xmlDoc.CreateElement("title");
xesub1.InnerText ="CSS禅意花园";//设置节点的文本值
xe1.AppendChild(xesub1);//添加到﹤book﹥节点中
XmlElement xesub2 = xmlDoc.CreateElement("author");
xesub2.InnerText ="Jeff";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("price");
xesub3.InnerText ="58.3";
xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到﹤bookshop﹥节点中
xmlDoc.Save("bookshop.xml"); //保存其更改
}
///<summary>
/// 修改节点
///</summary>
publicvoid UpdateNode()
{
xmlDoc =new XmlDocument();
xmlDoc.Load("bookshop.xml"); //加载xml文件
//获取bookshop节点的所有子节点
XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookshop").ChildNodes; //遍历所有子节点
foreach (XmlNode xn in nodeList) {
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 if (xe.GetAttribute("genre") =="Sky_Kwolf")//如果genre属性值为“Sky_Kwolf”
{
xe.SetAttribute("genre", "update Sky_Kwolf"); //则修改该属性为“update Sky_Kwolf”
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1; //转换类型
if (xe2.Name =="author")//如果找到
{
xe2.InnerText ="jason";//则修改
break;//找到退出
}
}
break;
}
} xmlDoc.Save("bookshop.xml");//保存。
}
//显示xml数据
publicvoid ShowXml()
{
xmlDoc =new XmlDocument();
xmlDoc.Load("bookshop.xml"); //加载xml文件
XmlNode xn = xmlDoc.SelectSingleNode("bookshop"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN")); XmlNodeList xnf1 = xe.ChildNodes;
foreach (XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}
}
///<summary>
/// 删除节点
///</summary>
publicvoid DeleteNode()
{
xmlDoc =new XmlDocument();
xmlDoc.Load("bookshop.xml"); //加载xml文件
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookshop").ChildNodes; foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("genre") =="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
elseif (xe.GetAttribute("genre") =="update Sky_Kwolf")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookshop.xml");
}
staticvoid Main(string[] args)
{ Program p =new Program();
p.ShowXml();
Console.ReadLine();
}
}
}

代码

XmlDocument XmlDoc = null;
XmlDoc = new XmlDocument(); XmlDeclaration dec = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlDoc.AppendChild(dec);
XmlElement root = XmlDoc.CreateElement("message");
XmlDoc.AppendChild(root); XmlElement eName;
XmlNode M_Root = XmlDoc.SelectSingleNode("message");
XmlNode M_Root1; XmlElement head = XmlDoc.CreateElement("header");
head.SetAttribute("checksum", "2e11193e4d27dc9b0600de7b2586b1e4");
root.AppendChild(head); eName = XmlDoc.CreateElement("bizid");//创建节点
eName.InnerXml = "";
head.AppendChild(eName); eName = XmlDoc.CreateElement("body");//创建节点
eName.InnerXml = "";
head.AppendChild(eName); XmlElement cont = XmlDoc.CreateElement("content");
root.AppendChild(cont);
M_Root1 = XmlDoc.SelectSingleNode("message").LastChild; XmlElement table = XmlDoc.CreateElement("Table");
table.SetAttribute("count", "");
cont.AppendChild(table); XmlElement row = XmlDoc.CreateElement("row");
row.SetAttribute("id", "");
table.AppendChild(row); eName = XmlDoc.CreateElement("column");//创建节点
eName.SetAttribute("type", "String");
eName.SetAttribute("id", "BT");
eName.SetAttribute("desc", "题名");
eName.InnerXml = "<![CDATA[" + ds.Tables[].Rows[i]["BT"].ToString() + "]]>";
row.AppendChild(eName); string file = FilePath + ds.Tables[].Rows[i]["ID"].ToString() + "REST.XML";
XmlDoc.Save(file); //保存XML return XmlDoc.InnerXml;//显示XML

C#读写xml文件的常用方法的更多相关文章

  1. PHP读写XML文件的四种方法

    PHP对XML文件进行读写操作的方法一共有四种,分别是:字符串方式直接读写.DOMDocument读写. XMLWrite写和XMLReader读.SimpleXML读写,本文将依次对这四种方法进行介 ...

  2. Java 读写XML文件 API--org.dom4j

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...

  3. Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  4. cocos2d-x 读写 xml 文件

    cocos2d-x 读写 xml 文件 A product of cheungmine使用cocos2d-x开发2d游戏确实方便,但是对于一般的小游戏,经常需要的工作是UI布局设计和调整,代码改来改去 ...

  5. java通过dom读写xml文件

    java通过dom读写xml文件 要读的xml文件 <?xml version="1.0" encoding="GB2312"?><学生花名册 ...

  6. C# 读写XML文件的方法

    C# 读写XML文件的方法 一.写XML文件 XmlDocument xmlDocument = new XmlDocument();xmlDocument.AppendChild(xmlDocume ...

  7. VC 使用msxml6.dll动态链接库中的函数读写XML文件

    VC 使用msxml6.dll动态链接库中的函数读写XML文件 目录 1 引言 2 .dll使用方法 3 常用函数总结 4 实例应用 5 运行效果预览 6 补充说明 7 不足之处 8 更新   引言: ...

  8. 【VS开发】【计算机视觉】OpenCV读写xml文件《C++版本》

    OpenCV FileStorage类读写XML/YML文件 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件,opencv2.0之前都是使用C风格的代码,当时读写XM ...

  9. Spring读写xml文件

    一.如果只是读取 新建一个 xml 文件,需要满足Spring格式: <?xml version="1.0" encoding="UTF-8"?> ...

随机推荐

  1. Redis 新特性---pipeline(管道)

    转载自http://weipengfei.blog.51cto.com/1511707/1215042 Redis本身是一个cs模式的tcp server, client可以通过一个socket连续发 ...

  2. Farey Sequence

    Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rationa ...

  3. 使用eclipse搭建hadoop开发环境

    下载一个 hadoop-eclipse-plugin-*.jar的eclipse插件,并放在plugins目录下 重启eclipse   打开视象,找“大象” 连接HDFS   success 编程准 ...

  4. Delphi七个版本

    第一次Zack Urlocker给我看一款尚未发布的名为Delphi的产品时,我意识到她将改变我的工作—还有许多其他软件开发者的工作. 我过去为使用Windows C++ 库而挣扎,而Delphi过去 ...

  5. EXT学习之——Ext两个js之间的传参

    A  的js访问 B的js,并将A选择的guid的行传到  B的 js进行处理事项 A 的js 的写法var receiverFrom = new xxx.xxx子js方法体名 ({ parentCm ...

  6. express 4 中 session的处理(仅为博主笔记)

    1.app.js var express = require('express') var app = express();var routers = require('./router/index' ...

  7. 实现Base64加密解密

    using System; using System.Text;   namespace Common { /// <summary> /// 实现Base64加密解密 /// </ ...

  8. cs11_c++_lab1

    lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...

  9. leetcode 169

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  10. afterTextChanged() callback being called without the text being actually changed

    afterTextChanged() callback being called without the text being actually changed up vote8down votefa ...