在C#.net中如何操作XML_转摘

在C#.net中如何操作XML
需要添加的命名空间:

None.gifusing System.Xml;

定义几个公共对象:

None.gifXmlDocument xmldoc ;
None.gifXmlNode xmlnode ;
None.gifXmlElement xmlelem ;

1,创建到服务器同名目录下的xml文件:
方法一:

None.gifxmldoc = new XmlDocument ( ) ;
None.gif
//加入XML的声明段落
None.gif
xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;
None.gifxmldoc.AppendChild ( xmlnode ) ;
None.gif
//加入一个根元素
None.gif
xmlelem = xmldoc.CreateElement ( "" , "Employees" , "" ) ;
None.gifxmldoc.AppendChild ( xmlelem ) ;
None.gif
//加入另外一个元素
None.gif
for(int i=1;i<3;i++)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gifXmlNode root
=xmldoc.SelectSingleNode("Employees");//查找<Employees> 
InBlock.gif
XmlElement xe1=xmldoc.CreateElement("Node");//创建一个<Node>节点 
InBlock.gif
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性 
InBlock.gif
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性 
InBlock.gif

InBlock.gifXmlElement xesub1
=xmldoc.CreateElement("title"); 
InBlock.gifxesub1.InnerText
="CS从入门到精通";//设置文本节点 
InBlock.gif
xe1.AppendChild(xesub1);//添加到<Node>节点中 
InBlock.gif
XmlElement xesub2=xmldoc.CreateElement("author"); 
InBlock.gifxesub2.InnerText
="候捷"
InBlock.gifxe1.AppendChild(xesub2); 
InBlock.gifXmlElement xesub3
=xmldoc.CreateElement("price"); 
InBlock.gifxesub3.InnerText
="58.3"
InBlock.gifxe1.AppendChild(xesub3); 
InBlock.gif
InBlock.gifroot.AppendChild(xe1);
//添加到<Employees>节点中 
ExpandedBlockEnd.gif
}

None.gif
//保存创建好的XML文档
None.gif
xmldoc.Save ( Server.MapPath("data.xml") ) ; 
None.gif

//////////////////////////////////////////////////////////////////////////////////////
结果:在同名目录下生成了名为data.xml的文件,内容如下,

None.gif<?xml version="1.0"?>
None.gif
<Employees>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif
</Employees>
None.gif

方法二:

None.gifXmlTextWriter xmlWriter;
None.gif   
string strFilename = Server.MapPath("data1.xml") ;
None.gif
None.gif   xmlWriter 
= new XmlTextWriter(strFilename,Encoding.Default);//创建一个xml文档
None.gif
   xmlWriter.Formatting = Formatting.Indented;
None.gif   xmlWriter.WriteStartDocument();
None.gif   xmlWriter.WriteStartElement(
"Employees");
None.gif
None.gif   xmlWriter.WriteStartElement(
"Node");
None.gif   xmlWriter.WriteAttributeString(
"genre","李赞红");
None.gif   xmlWriter.WriteAttributeString(
"ISBN","2-3631-4");
None.gif
None.gif   xmlWriter.WriteStartElement(
"title");
None.gif   xmlWriter.WriteString(
"CS从入门到精通");
None.gif   xmlWriter.WriteEndElement();
None.gif
None.gif   xmlWriter.WriteStartElement(
"author");
None.gif   xmlWriter.WriteString(
"候捷");
None.gif   xmlWriter.WriteEndElement();
None.gif
None.gif   xmlWriter.WriteStartElement(
"price");
None.gif   xmlWriter.WriteString(
"58.3");
None.gif   xmlWriter.WriteEndElement();
None.gif
None.gif   xmlWriter.WriteEndElement();
None.gif
None.gif   xmlWriter.Close();
None.gif
None.gif


//////////////////////////////////////////////////////////////////////////////////////
结果:

None.gif<?xml version="1.0" encoding="gb2312"?>
None.gif
<Employees>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif
</Employees>
None.gif

2,添加一个结点:

 

None.gifXmlDocument xmlDoc=new XmlDocument(); 
None.gifxmlDoc.Load(Server.MapPath(
"data.xml")); 
None.gifXmlNode root
=xmlDoc.SelectSingleNode("Employees");//查找<Employees> 
None.gif
XmlElement xe1=xmlDoc.CreateElement("Node");//创建一个<Node>节点 
None.gif
xe1.SetAttribute("genre","张三");//设置该节点genre属性 
None.gif
xe1.SetAttribute("ISBN","1-1111-1");//设置该节点ISBN属性 
None.gif

None.gifXmlElement xesub1
=xmlDoc.CreateElement("title"); 
None.gifxesub1.InnerText
="C#入门帮助";//设置文本节点 
None.gif
xe1.AppendChild(xesub1);//添加到<Node>节点中 
None.gif
XmlElement xesub2=xmlDoc.CreateElement("author"); 
None.gifxesub2.InnerText
="高手"
None.gifxe1.AppendChild(xesub2); 
None.gifXmlElement xesub3
=xmlDoc.CreateElement("price"); 
None.gifxesub3.InnerText
="158.3"
None.gifxe1.AppendChild(xesub3); 
None.gif
None.gifroot.AppendChild(xe1);
//添加到<Employees>节点中 
None.gif
xmlDoc.Save ( Server.MapPath("data.xml") );
None.gif
None.gif

//////////////////////////////////////////////////////////////////////////////////////
结果:在xml原有的内容里添加了一个结点,内容如下,

None.gif<?xml version="1.0"?>
None.gif
<Employees>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="张三" ISBN="1-1111-1">
None.gif    
<title>C#入门帮助</title>
None.gif    
<author>高手</author>
None.gif    
<price>158.3</price>
None.gif  
</Node>
None.gif
</Employees>
None.gif

3,修改结点的值(属性和子结点):

None.gifXmlDocument xmlDoc=new XmlDocument(); 
None.gifxmlDoc.Load( Server.MapPath(
"data.xml") ); 
None.gif
None.gifXmlNodeList nodeList
=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点 
None.gif

None.gif
foreach(XmlNode xn in nodeList)//遍历所有子节点 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gifXmlElement xe
=(XmlElement)xn;//将子节点类型转换为XmlElement类型 
InBlock.gif
if(xe.GetAttribute("genre")=="张三")//如果genre属性值为“张三” 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifxe.SetAttribute(
"genre","update张三");//则修改该属性为“update张三” 
InBlock.gif

InBlock.gifXmlNodeList nls
=xe.ChildNodes;//继续获取xe子节点的所有子节点 
InBlock.gif
foreach(XmlNode xn1 in nls)//遍历 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifXmlElement xe2
=(XmlElement)xn1;//转换类型 
InBlock.gif
if(xe2.Name=="author")//如果找到 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifxe2.InnerText
="亚胜";//则修改
ExpandedSubBlockEnd.gif
}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gifxmlDoc.Save( Server.MapPath(
"data.xml") );//保存。
None.gif

//////////////////////////////////////////////////////////////////////////////////////
结果:将原来的所有结点的信息都修改了,xml的内容如下,

None.gif<?xml version="1.0"?>
None.gif
<Employees>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="update湃?quot; ISBN="1-1111-1">
None.gif    
<title>C#入门帮助</title>
None.gif    
<author>亚胜</author>
None.gif    
<price>158.3</price>
None.gif  
</Node>
None.gif
</Employees>

4,修改结点(添加结点的属性和添加结点的自结点):

None.gifXmlDocument xmlDoc=new XmlDocument(); 
None.gifxmlDoc.Load( Server.MapPath(
"data.xml") ); 
None.gif
None.gifXmlNodeList nodeList
=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点 
None.gif

None.gif
foreach(XmlNode xn in nodeList) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gifXmlElement xe
=(XmlElement)xn; 
InBlock.gifxe.SetAttribute(
"test","111111");
InBlock.gif
InBlock.gifXmlElement xesub
=xmlDoc.CreateElement("flag"); 
InBlock.gifxesub.InnerText
="1"
InBlock.gifxe.AppendChild(xesub); 
ExpandedBlockEnd.gif}
 
None.gifxmlDoc.Save( Server.MapPath(
"data.xml") );
None.gif
None.gif

//////////////////////////////////////////////////////////////////////////////////////
结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下,

None.gif<?xml version="1.0"?>
None.gif
<Employees>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif    
<flag>1</flag>
None.gif  
</Node>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4" test="111111">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif    
<flag>1</flag>
None.gif  
</Node>
None.gif  
<Node genre="update张三" ISBN="1-1111-1" test="111111">
None.gif    
<title>C#入门帮助</title>
None.gif    
<author>亚胜</author>
None.gif    
<price>158.3</price>
None.gif    
<flag>1</flag>
None.gif  
</Node>
None.gif
</Employees>
None.gif

5,删除结点中的某一个属性:

None.gifXmlDocument xmlDoc=new XmlDocument(); 
None.gifxmlDoc.Load( Server.MapPath(
"data.xml") ); 
None.gifXmlNodeList xnl
=xmlDoc.SelectSingleNode("Employees").ChildNodes; 
None.gif
foreach(XmlNode xn in xnl) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gifXmlElement xe
=(XmlElement)xn; 
InBlock.gifxe.RemoveAttribute(
"genre");//删除genre属性 
InBlock.gif

InBlock.gifXmlNodeList nls
=xe.ChildNodes;//继续获取xe子节点的所有子节点 
InBlock.gif
foreach(XmlNode xn1 in nls)//遍历 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifXmlElement xe2
=(XmlElement)xn1;//转换类型 
InBlock.gif
if(xe2.Name=="flag")//如果找到 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifxe.RemoveChild(xe2);
//则删除
ExpandedSubBlockEnd.gif
}
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gifxmlDoc.Save( Server.MapPath(
"data.xml") ); 
None.gif
None.gif

//////////////////////////////////////////////////////////////////////////////////////]
结果:删除了结点的一个属性和结点的一个子结点,内容如下,

None.gif<?xml version="1.0"?>
None.gif
<Employees>
None.gif  
<Node ISBN="2-3631-4" test="111111">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node ISBN="2-3631-4" test="111111">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node ISBN="1-1111-1" test="111111">
None.gif    
<title>C#入门帮助</title>
None.gif    
<author>亚胜</author>
None.gif    
<price>158.3</price>
None.gif  
</Node>
None.gif
</Employees>
None.gif

6,删除结点:

None.gifXmlDocument xmlDoc=new XmlDocument(); 
None.gifxmlDoc.Load( Server.MapPath(
"data.xml") ); 
None.gifXmlNode root
=xmlDoc.SelectSingleNode("Employees");
None.gifXmlNodeList xnl
=xmlDoc.SelectSingleNode("Employees").ChildNodes; 
None.gif
for(int i=0;i<xnl.Count;i++)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifXmlElement xe
=(XmlElement)xnl.Item(i); 
InBlock.gif
if(xe.GetAttribute("genre")=="张三"
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifroot.RemoveChild(xe);
InBlock.gif
if(i<xnl.Count)i=i-1;
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}

None.gifxmlDoc.Save( Server.MapPath(
"data.xml") ); 
None.gif

//////////////////////////////////////////////////////////////////////////////////////]
结果:删除了符合条件的所有结点,原来的内容:

 

None.gif<?xml version="1.0"?>
None.gif
<Employees>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="张三" ISBN="1-1111-1">
None.gif    
<title>C#入门帮助</title>
None.gif    
<author>高手</author>
None.gif
<price>158.3</price>
None.gif  
</Node>
None.gif  
<Node genre="张三" ISBN="1-1111-1">
None.gif    
<title>C#入门帮助</title>
None.gif    
<author>高手</author>
None.gif    
<price>158.3</price>
None.gif  
</Node>
None.gif
</Employees>
None.gif
None.gif

删除后的内容:

None.gif<?xml version="1.0"?>
None.gif
<Employees>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif  
<Node genre="李赞红" ISBN="2-3631-4">
None.gif    
<title>CS从入门到精通</title>
None.gif    
<author>候捷</author>
None.gif    
<price>58.3</price>
None.gif  
</Node>
None.gif
</Employees>

转载于:https://www.cnblogs.com/wlq2000/archive/2006/12/12/589706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值