C#读写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文件(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();
}
}
}


 

<?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>


 

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();
}
}
}


 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值