C#中XML的基本操作
来源:互联网 发布:2017公安部网络贩枪案 编辑:程序博客网 时间:2024/06/05 18:56
我用的是一种很笨的方法,但可以帮助初学者了解访问XML节点的过程.
已知有一个XML文件(bookstore.xml)如下:
<?xml version= encoding=?><bookstore> <book genre=ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book></bookstore>
1、往<bookstore>节点中插入一个<book>节点:
XmlDocument xmlDoc=new XmlDocument( );
xmlDoc.Load( );
XmlNode root=xmlDoc.SelectSingleNode( );
//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement( );
//创建一个<book>节点
xe1.SetAttribute( , );
//设置该节点genre属性
xe1.SetAttribute( , );
//设置该节点ISBN属性
XmlElement xesub1=xmlDoc.CreateElement( );
xesub1.InnerText=;
//设置文本节点
xe1.AppendChild( xesub1 );
//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement( );
xesub2.InnerText=;
xe1.AppendChild( xesub2 );
XmlElement xesub3=xmlDoc.CreateElement( );
xesub3.InnerText=;
xe1.AppendChild( xesub3 );
root.AppendChild( xe1 );
//添加到<bookstore>节点中
xmlDoc.Save( );
//===============================================
结果为:
<?xml version= encoding=?><bookstore> <book genre= ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book> <book genre= ISBN=>
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price> </book></bookstore>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”.
XmlNodeList nodeList=xmlDoc.SelectSingleNode( ).ChildNodes;
//获取bookstore节点的所有子节点
foreach( XmlNode xn in nodeList )//遍历所有子节点
{
XmlElement xe=( XmlElement )xn;
//将子节点类型转换为XmlElement类型
if( xe.GetAttribute( )== )//如果genre属性值为“李赞红”
{
xe.SetAttribute( , );
//则修改该属性为“update李赞红”
XmlNodeList nls=xe.ChildNodes;
//继续获取xe子节点的所有子节点
foreach( XmlNode xn1 in nls )//遍历
{
XmlElement xe2=( XmlElement )xn1;
//转换类型
if( xe2.Name== )//如果找到
{
xe2.InnerText=;
//则修改
break;
//找到退出来就可以了
}
}
break;
}
}
xmlDoc.Save( );
//保存.
//==================================================
最后结果为:
<?xml version= encoding=?><bookstore> <book genre= ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book> <book genre= ISBN=>
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price> </book></bookstore>
3、删除 <book genre= ISBN=>节点的genre属性,删除 <book genre=ISBN=>节点.
XmlNodeList xnl=xmlDoc.SelectSingleNode( ).ChildNodes;
foreach( XmlNode xn in xnl )
{
XmlElement xe=( XmlElement )xn;
if( xe.GetAttribute( )== )
{
xe.RemoveAttribute( );
//删除genre属性
}
else if( xe.GetAttribute( )== )
{
xe.RemoveAll( );
//删除该节点的全部内容
}
}
xmlDoc.Save( );
//===========================================最后结果为:
<?xml version= encoding=?><bookstore> <book ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book> <book> </book></bookstore>
4、显示所有数据.
XmlNode xn=xmlDoc.SelectSingleNode( );
XmlNodeList xnl=xn.ChildNodes;
foreach( XmlNode xnf in xnl )
{
XmlElement xe=( XmlElement )xnf;
Console.WriteLine( xe.GetAttribute( ) );
//显示属性值
Console.WriteLine( xe.GetAttribute( ) );
XmlNodeList xnf1=xe.ChildNodes;
foreach( XmlNode xn2 in xnf1 )
{
Console.WriteLine( xn2.InnerText );
//显示子节点点文本
}
}
已知有一个XML文件(bookstore.xml)如下:
<?xml version= encoding=?><bookstore> <book genre=ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book></bookstore>
1、往<bookstore>节点中插入一个<book>节点:
XmlDocument xmlDoc=new XmlDocument( );
xmlDoc.Load( );
XmlNode root=xmlDoc.SelectSingleNode( );
//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement( );
//创建一个<book>节点
xe1.SetAttribute( , );
//设置该节点genre属性
xe1.SetAttribute( , );
//设置该节点ISBN属性
XmlElement xesub1=xmlDoc.CreateElement( );
xesub1.InnerText=;
//设置文本节点
xe1.AppendChild( xesub1 );
//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement( );
xesub2.InnerText=;
xe1.AppendChild( xesub2 );
XmlElement xesub3=xmlDoc.CreateElement( );
xesub3.InnerText=;
xe1.AppendChild( xesub3 );
root.AppendChild( xe1 );
//添加到<bookstore>节点中
xmlDoc.Save( );
//===============================================
结果为:
<?xml version= encoding=?><bookstore> <book genre= ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book> <book genre= ISBN=>
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price> </book></bookstore>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”.
XmlNodeList nodeList=xmlDoc.SelectSingleNode( ).ChildNodes;
//获取bookstore节点的所有子节点
foreach( XmlNode xn in nodeList )//遍历所有子节点
{
XmlElement xe=( XmlElement )xn;
//将子节点类型转换为XmlElement类型
if( xe.GetAttribute( )== )//如果genre属性值为“李赞红”
{
xe.SetAttribute( , );
//则修改该属性为“update李赞红”
XmlNodeList nls=xe.ChildNodes;
//继续获取xe子节点的所有子节点
foreach( XmlNode xn1 in nls )//遍历
{
XmlElement xe2=( XmlElement )xn1;
//转换类型
if( xe2.Name== )//如果找到
{
xe2.InnerText=;
//则修改
break;
//找到退出来就可以了
}
}
break;
}
}
xmlDoc.Save( );
//保存.
//==================================================
最后结果为:
<?xml version= encoding=?><bookstore> <book genre= ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book> <book genre= ISBN=>
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price> </book></bookstore>
3、删除 <book genre= ISBN=>节点的genre属性,删除 <book genre=ISBN=>节点.
XmlNodeList xnl=xmlDoc.SelectSingleNode( ).ChildNodes;
foreach( XmlNode xn in xnl )
{
XmlElement xe=( XmlElement )xn;
if( xe.GetAttribute( )== )
{
xe.RemoveAttribute( );
//删除genre属性
}
else if( xe.GetAttribute( )== )
{
xe.RemoveAll( );
//删除该节点的全部内容
}
}
xmlDoc.Save( );
//===========================================最后结果为:
<?xml version= encoding=?><bookstore> <book ISBN=>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> </book> <book> </book></bookstore>
4、显示所有数据.
XmlNode xn=xmlDoc.SelectSingleNode( );
XmlNodeList xnl=xn.ChildNodes;
foreach( XmlNode xnf in xnl )
{
XmlElement xe=( XmlElement )xnf;
Console.WriteLine( xe.GetAttribute( ) );
//显示属性值
Console.WriteLine( xe.GetAttribute( ) );
XmlNodeList xnf1=xe.ChildNodes;
foreach( XmlNode xn2 in xnf1 )
{
Console.WriteLine( xn2.InnerText );
//显示子节点点文本
}
}
- C#中XML的基本操作
- C#中XML的基本操作
- C#中XML的基本操作
- C#中XML的基本操作
- C#中XML的基本操作
- C#中对XML文件的一些基本操作
- C# XML基本操作
- C# XML基本操作
- c# XML基本操作
- c#对xml的支持基本操作
- XML数据的基本操作汇总C#
- c#对xml的基本操作
- C#操作Xml的基本方法
- C#对XML的基本操作
- C#操作XML(基本操作)
- C#中XML的基本应用
- C#中XML的基本应用
- .net中xml文件的基本操作
- Android4.0之前HttpClient的问题The target server failed to respond
- 学会举一反三,扩展更多功能
- 高效算法设计专项:LA 2689
- C# 程序员参考--数组教程
- windows 编程 开始了 ···
- C#中XML的基本操作
- 设置用户ID位的那回事
- 1、重写TextView的onDraw方法
- tar详解及常用命令
- 论.NET反射、委托技术与设计模式关系
- 开平方 如果没有计算器,我们如何求2的平方根? 可以先猜测一个数,比如1.5,然后用2除以这个数字。
- eclipse3.6安装tomcat插件
- C#中的数据绑定
- 单链表应用之多项式加法