当前位置: 移动技术网 > IT编程>开发语言>c# > asp.net中XML如何做增删改查操作

asp.net中XML如何做增删改查操作

2019年07月18日  | 移动技术网IT编程  | 我要评论
一、简单介绍 using system.xml; //初始化一个xml实例 xmldocument xml=new xmldocument(); //导入指

一、简单介绍

using system.xml;
//初始化一个xml实例
xmldocument xml=new xmldocument();
//导入指定xml文件
xml.load(path);
xml.load(httpcontext.current.server.mappath("~/file/bookstore.xml"));
//指定一个节点
xmlnode root=xml.selectsinglenode("/root");
//获取节点下所有直接子节点
xmlnodelist childlist=root.childnodes;
//判断该节点下是否有子节点
root.haschildnodes;
//获取同名同级节点集合
xmlnodelist nodelist=xml.selectnodes("/root/news");
//生成一个新节点
xmlelement node=xml.createelement("news");
//将节点加到指定节点下,作为其子节点
root.appendchild(node);
//将节点加到指定节点下某个子节点前
root.insertbefore(node,root.childenodes[i]);
//为指定节点的新建属性并赋值
node.setattribute("id","11111");
//为指定节点添加子节点
root.appendchild(node);
//获取指定节点的指定属性值
string id=node.attributes["id"].value;
//获取指定节点中的文本
string content=node.innertext;
//保存xml文件
string path=server.mappath("~/file/bookstore.xml");
xml.save(path);
//or use :xml.save(httpcontext.current.server.mappath("~/file/bookstore.xml")); 

二、具体实例

在c#.net中如何操作xml

需要添加的命名空间:

using system.xml;

定义几个公共对象:

xmldocument xmldoc ;
xmlnode xmlnode ;
xmlelement xmlelem ;

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

方法一:

xmldoc = new xmldocument ( ) ;
//加入xml的声明段落,<?xml version="1.0" encoding="gb2312"?>
xmldeclaration xmldecl;
 xmldecl = xmldoc.createxmldeclaration("1.0","gb2312",null);
 xmldoc.appendchild ( xmldecl);

//加入一个根元素
xmlelem = xmldoc.createelement ( "" , "employees" , "" ) ;
xmldoc.appendchild ( xmlelem ) ;
//加入另外一个元素
for(int i=1;i<3;i++)
{

xmlnode root=xmldoc.selectsinglenode("employees");//查找<employees> 
xmlelement xe1=xmldoc.createelement("node");//创建一个<node>节点 
xe1.setattribute("genre","李赞红");//设置该节点genre属性 
xe1.setattribute("isbn","2-3631-4");//设置该节点isbn属性

xmlelement xesub1=xmldoc.createelement("title"); 
xesub1.innertext="cs从入门到精通";//设置文本节点 
xe1.appendchild(xesub1);//添加到<node>节点中 
xmlelement xesub2=xmldoc.createelement("author"); 
xesub2.innertext="候捷"; 
xe1.appendchild(xesub2); 
xmlelement xesub3=xmldoc.createelement("price"); 
xesub3.innertext="58.3"; 
xe1.appendchild(xesub3);

root.appendchild(xe1);//添加到<employees>节点中 
}
//保存创建好的xml文档
xmldoc.save ( server.mappath("data.xml") ) ;

//////////////////////////////////////////////////////////////////////////////////////

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

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
</employees>

方法二:

xmltextwriter xmlwriter;
 string strfilename = server.mappath("data1.xml") ;
  xmlwriter = new xmltextwriter(strfilename,encoding.default);//创建一个xml文档
  xmlwriter.formatting = formatting.indented;
  xmlwriter.writestartdocument();
  xmlwriter.writestartelement("employees");
  xmlwriter.writestartelement("node");
  xmlwriter.writeattributestring("genre","李赞红");
  xmlwriter.writeattributestring("isbn","2-3631-4");
  xmlwriter.writestartelement("title");
  xmlwriter.writestring("cs从入门到精通");
  xmlwriter.writeendelement();
  xmlwriter.writestartelement("author");
  xmlwriter.writestring("候捷");
  xmlwriter.writeendelement();
  xmlwriter.writestartelement("price");
  xmlwriter.writestring("58.3");
  xmlwriter.writeendelement();
  xmlwriter.writeendelement();
  xmlwriter.close();

//////////////////////////////////////////////////////////////////////////////////////

结果:

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
</employees>

2,添加一个结点:

xmldocument xmldoc=new xmldocument(); 
xmldoc.load(server.mappath("data.xml")); 
xmlnode root=xmldoc.selectsinglenode("employees");//查找<employees> 
xmlelement xe1=xmldoc.createelement("node");//创建一个<node>节点 
xe1.setattribute("genre","张三");//设置该节点genre属性 
xe1.setattribute("isbn","1-1111-1");//设置该节点isbn属性
xmlelement xesub1=xmldoc.createelement("title"); 
xesub1.innertext="c#入门帮助";//设置文本节点 
xe1.appendchild(xesub1);//添加到<node>节点中 
xmlelement xesub2=xmldoc.createelement("author"); 
xesub2.innertext="高手"; 
xe1.appendchild(xesub2); 
xmlelement xesub3=xmldoc.createelement("price"); 
xesub3.innertext="158.3"; 
xe1.appendchild(xesub3);
root.appendchild(xe1);//添加到<employees>节点中 
xmldoc.save ( server.mappath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////

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

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="张三" isbn="1-1111-1">
  <title>c#入门帮助</title>
  <author>高手</author>
  <price>158.3</price>
 </node>
</employees>

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

xmldocument xmldoc=new xmldocument(); 
xmldoc.load( server.mappath("data.xml") );
xmlnodelist nodelist=xmldoc.selectsinglenode("employees").childnodes;//获取employees节点的所有子节点
foreach(xmlnode xn in nodelist)//遍历所有子节点 
{ 
xmlelement xe=(xmlelement)xn;//将子节点类型转换为xmlelement类型 
if(xe.getattribute("genre")=="张三")//如果genre属性值为“张三” 
{ 
xe.setattribute("genre","update张三");//则修改该属性为“update张三”
xmlnodelist nls=xe.childnodes;//继续获取xe子节点的所有子节点 
foreach(xmlnode xn1 in nls)//遍历 
{ 
xmlelement xe2=(xmlelement)xn1;//转换类型 
if(xe2.name=="author")//如果找到 
{ 
xe2.innertext="亚胜";//则修改
} 
} 
} 
} 
xmldoc.save( server.mappath("data.xml") );//保存。

//////////////////////////////////////////////////////////////////////////////////////

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

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="update张三" isbn="1-1111-1">
  <title>c#入门帮助</title>
  <author>亚胜</author>
  <price>158.3</price>
 </node>
</employees>

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

xmldocument xmldoc=new xmldocument(); 
xmldoc.load( server.mappath("data.xml") );
xmlnodelist nodelist=xmldoc.selectsinglenode("employees").childnodes;//获取employees节点的所有子节点
foreach(xmlnode xn in nodelist) 
{ 
xmlelement xe=(xmlelement)xn; 
xe.setattribute("test","111111");
xmlelement xesub=xmldoc.createelement("flag"); 
xesub.innertext="1"; 
xe.appendchild(xesub); 
} 
xmldoc.save( server.mappath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////

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

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node genre="李赞红" isbn="2-3631-4" test="111111">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
  <flag>1</flag>
 </node>
 <node genre="李赞红" isbn="2-3631-4" test="111111">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
  <flag>1</flag>
 </node>
 <node genre="update张三" isbn="1-1111-1" test="111111">
  <title>c#入门帮助</title>
  <author>亚胜</author>
  <price>158.3</price>
  <flag>1</flag>
 </node>
</employees>

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

xmldocument xmldoc=new xmldocument(); 
xmldoc.load( server.mappath("data.xml") ); 
xmlnodelist xnl=xmldoc.selectsinglenode("employees").childnodes; 
foreach(xmlnode xn in xnl) 
{ 
xmlelement xe=(xmlelement)xn; 
xe.removeattribute("genre");//删除genre属性
xmlnodelist nls=xe.childnodes;//继续获取xe子节点的所有子节点 
foreach(xmlnode xn1 in nls)//遍历 
{ 
xmlelement xe2=(xmlelement)xn1;//转换类型 
if(xe2.name=="flag")//如果找到 
{ 
xe.removechild(xe2);//则删除
} 
} 
} 
xmldoc.save( server.mappath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////]

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

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node isbn="2-3631-4" test="111111">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node isbn="2-3631-4" test="111111">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node isbn="1-1111-1" test="111111">
  <title>c#入门帮助</title>
  <author>亚胜</author>
  <price>158.3</price>
 </node>
</employees>

6,删除结点:

xmldocument xmldoc=new xmldocument(); 
xmldoc.load( server.mappath("data.xml") ); 
xmlnode root=xmldoc.selectsinglenode("employees");
xmlnodelist xnl=xmldoc.selectsinglenode("employees").childnodes; 
for(int i=0;i<xnl.count;i++)
{
xmlelement xe=(xmlelement)xnl.item(i); 
if(xe.getattribute("genre")=="张三") 
{ 
root.removechild(xe);
if(i<xnl.count)i=i-1;
} 
}
xmldoc.save( server.mappath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////]

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

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="张三" isbn="1-1111-1">
  <title>c#入门帮助</title>
  <author>高手</author>
  <price>158.3</price>
 </node>
 <node genre="张三" isbn="1-1111-1">
  <title>c#入门帮助</title>
  <author>高手</author>
  <price>158.3</price>
 </node>
</employees>

删除后的内容:

<?xml version="1.0" encoding="gb2312"?>
<employees>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
 <node genre="李赞红" isbn="2-3631-4">
  <title>cs从入门到精通</title>
  <author>候捷</author>
  <price>58.3</price>
 </node>
</employees>

 7,按照文本文件读取xml

system.io.streamreader myfile =new 
system.io.streamreader(server.mappath("data.xml"),system.text.encoding.default);
//注意system.text.encoding.default
string mystring = myfile.readtoend();//mystring是读出的字符串
myfile.close();

三、高级应用

/*读取xml数据  两种xml方式*/
<aaa>
   <bb>something</bb>
   <cc>something</cc>
</aaa>
<aaa>
  <add key="123" value="321"/>
</aaa>

/*第一种方法*/

ds.readxml("your xmlfile name");
container.dataitem("bb");
container.dataitem("cc");
ds.readxmlschema("your xmlfile name");

/*第二种方法*/

<aaa>
  <add key="123" value="321"/>
</aaa>

如果我要找到123然后取到321应该怎么写呢?

using system.xml;
xmldatadocument xmldoc = new system.xml.xmldatadocument();
xmldoc.load(@"c:\config.xml");
xmlelement elem = xmldoc.getelementbyid("add");
string str = elem.attributes["value"].value

/*第三种方法:  selectsinglenode  读取两种格式的xml *---/

--------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appsettings>
    <connectionstring>data source=yf; user id=ctm_dbo;password=123</connectionstring>       
 </appsettings>
</configuration>
--------------------------------------------------------------------------
xmldocument doc = new xmldocument();
doc.load(strxmlname);
 
  xmlnode node=doc.selectsinglenode("/configuration/appsettings/connectionstring");
  if(node!=null)
  {
   string k1=node.value;  //null
   string k2=node.innertext;//data source=yf; user id=ctm_dbo;password=123
   string k3=node.innerxml;//data source=yf; user id=ctm_dbo;password=123
   node=null;
  }
 
********************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appsettings>
    <add key="connectionstring" value="data source=yf; user id=ctm_dbo;password=123" />       
 </appsettings>
</configuration>
**--------------------------------------------------------------------**
   xmlnode node=doc.selectsinglenode("/configuration/appsettings/add");
  if(node!=null)
  {
   string k=node.attributes["key"].value;
   string v=node.attributes["value"].value;
   node=null;
  }
*--------------------------------------------------------------------*
  xmlnode node=doc.selectsinglenode("/configuration/appsettings/add");
  if(node!=null)
  {
   xmlnodereader nr=new xmlnodereader(node);
   nr.movetocontent();
  //检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
   nr.movetoattribute("value");
   string s=nr.value;
   node=null;
  }

以上内容就是asp.net中xml如何做增删改查操作,希望大家喜欢。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网