使用XmlDocument类给xml文件元素增加属性

            XmlDocument doc = new XmlDocument();
            doc.Load("library.xml");

            XmlNodeList movies = doc.SelectNodes("//movie");
            XmlAttribute attribute = doc.CreateAttribute("damaged");
            attribute.Value = "no";
            
             foreach (XmlNode item in movies)
            {
                item.Attributes.Append(attribute);               
            }

XML文件如下:

<?xml version="1.0" ?>
<library xmlns:network="www.library.com">
	<books>
	<book checkout="no">
		<title>To Kill a Mockingbird1</title>
			<author>232323_1_lee</author>
			</book>
	<book checkout="no">
		<title> To Kill a Mockingbird2</title>                     
			<author>232323_2_lee</author>
			</book>
	<book checkout="yes">
		<title> To Kill a Mockingbird3</title>
			<author>232323_3_lee</author>
			</book>
	<book checkout="no">
		<title> To Kill a Mockingbird4</title>
			<author>232323_4_lee</author>
			</book>
	</books>			
 <movies>	
	  <movie checkedout="no">
	   <title>King Kong1</title>
	   <year>1933</year>
	  </movie>
	  	  <movie checkedout="no">
	   <title>King Kong2</title>
	   <year>2933</year>
	  </movie>
	  	  <movie checkedout="yes">
	   <title>King Kong3</title>
	   <year>3933</year>
	  </movie>
	  	  <movie checkedout="no">
	   <title>King Kong4</title>
	   <year>4933</year>
	  </movie>
	  	  <movie checkedout="no">
	   <title>King Kong5</title>
	   <year>5933</year>
	  </movie>
	   </movies> 
</library>
	   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值