这里主要讲xmldocument创建,删除,编辑,插入节点的xml操作。
xmldocument xmldoc = new xmldocument();
xmlnode xmlnode;
xmlelement xmlelem;
xmlelement xmlelem2;
xmltext xmltext;
//加入xml的声明段落
xmlnode = xmldoc.createnode(xmlnodetype.xmldeclaration, "", "");
xmldoc.appendchild(xmlnode);
//加入一个根元素
xmlelem = xmldoc.createelement("", "root", "");
xmltext = xmldoc.createtextnode("root text");
xmlelem.appendchild(xmltext);
xmldoc.appendchild(xmlelem);
//加入另外一个元素
xmlelem2 = xmldoc.createelement("sampleelement");
xmlelem2 = xmldoc.createelement("", "sampleelement", "");
xmltext = xmldoc.createtextnode("the text of the sample element");
xmlelem2.appendchild(xmltext);
xmldoc.childnodes.item(1).appendchild(xmlelem2);
//保存创建好的xml文档
try
{
xmldoc.save(" g:/yunli/srcloud.web/uploads/filehistory/data.xml");
}
catch (exception e)
{
//显示错误信息
console.writeline(e.message);
}
console.readline();
}
上面只是简单的创建,下面这个实例,xmldocument创建xml文档及增加删除更新节点
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.xml;
namespace xmldomdemo
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
private void btnload_click(object sender, eventargs e)
{
xmldocument xmldoc = new xmldocument();
xmldoc.load("books.xml");
messagebox.show(xmldoc.innerxml);
}
//创建文档
private void btncreate_click(object sender, eventargs e)
{
xmldocument xmldoc = new xmldocument();//建立xml的定义声明
xmldeclaration dec = xmldoc.createxmldeclaration("1.0", "gb2312", null);
xmldoc.appendchild(dec);//创建根节点
xmlelement root = xmldoc.createelement("books");
xmldoc.appendchild(root);
xmlnode book = xmldoc.createelement("book");xmlelement title = xmldoc.createelement("title");
title.innertext = "sql server";
book.appendchild(title);xmlelement isbn = xmldoc.createelement("isbn");
isbn.innertext = "444444";
book.appendchild(isbn);xmlelement author = xmldoc.createelement("author");
author.innertext = "jia";
book.appendchild(author);xmlelement price = xmldoc.createelement("price");
price.innertext = "120";
price.setattribute("unit", "___fckpd___0quot;);book.appendchild(price);
root.appendchild(book);xmldoc.save("books.xml");
}private void btninsert_click(object sender, eventargs e)
{
xmldocument xmldoc = new xmldocument();
xmldoc.load("books.xml");xmlnode root = xmldoc.selectsinglenode("books");
xmlelement book = xmldoc.createelement("book");
xmlelement title = xmldoc.createelement("title");
title.innertext = "xml";
book.appendchild(title);xmlelement isbn = xmldoc.createelement("isbn");
isbn.innertext = "333333";
book.appendchild(isbn);xmlelement author = xmldoc.createelement("author");
author.innertext = "snow";
book.appendchild(author);xmlelement price = xmldoc.createelement("price");
price.innertext = "120";
price.setattribute("unit", "___fckpd___0quot;);book.appendchild(price);
root.appendchild(book);
xmldoc.save("books.xml");
messagebox.show("数据已写入!");
}private void btnupdate_click(object sender, eventargs e)
{
xmldocument xmldoc = new xmldocument();
xmldoc.load("books.xml");//"//book[@unit="$"]"
//获取books节点的所有子节点
xmlnodelist nodelist = xmldoc.selectsinglenode("books//book").childnodes;//遍历所有子节点
foreach (xmlnode xn in nodelist)
{
//将子节点类型转换为xmlelement类型
xmlelement xe = (xmlelement)xn;
if (xe.name == "author")
{
xe.innertext = "amandag";
}if (xe.getattribute("unit") == "___fckpd___0quot;)
{
xe.setattribute("unit", "¥");
}
//break;
}//xmlnodelist nodelist = xmldoc.selectnodes("books//book");
//foreach (xmlnode xn in nodelist)
//{
// foreach (xmlnode x in xn.childnodes)
// {
// //将子节点类型转换为xmlelement类型
// xmlelement xe = (xmlelement)x;
// if (xe.name == "author")
// {
// xe.innertext = "amandag";
// }// if (xe.getattribute("unit") == "___fckpd___0quot;)
// {
// xe.setattribute("unit", "¥");
// }
// //break;
// }
//}
xmldoc.save("books.xml");
}private void btndelete_click(object sender, eventargs e)
{
xmldocument xmldoc = new xmldocument();
xmldoc.load("books.xml");xmlnodelist nodelist = xmldoc.selectsinglenode("books//book").childnodes;
//遍历所有子节点
foreach (xmlnode xn in nodelist)
{
//将子节点类型转换为xmlelement类型
xmlelement xe = (xmlelement)xn;
if (xe.name == "author")
{
xe.removeall();
}if (xe.getattribute("unit") == "¥")
{
xe.removeattribute("unit");
}
}
xmldoc.save("books.xml");
}
}
}
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮