asp.net操作xml问题

2024年11月18日 06:19
有3个网友回答
网友(1):

这个要测试太麻烦了。。
不过我这边有实例,我测试过可以使用,你可以参照一下。。学会了再来改这个吧!!~

///


/// 添加记录
///

///
///
private void button1_Click(object sender, EventArgs e)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("bookstore.xml"); // 载入xml文件
XmlNode root = xmlDoc.SelectSingleNode("bookstore");// 查找
XmlElement xe1 = xmlDoc.CreateElement("book"); // 创建一个节点

xe1.SetAttribute("genre", genre.Text); // 设置该节点genre属性
xe1.SetAttribute("ISBN", ISBN.Text); // 设置该节点ISBN属性

XmlElement xesub1 = xmlDoc.CreateElement("title"); // 创建一个节点<br> xesub1.InnerText = title.Text; // 设置文本节点<br> xe1.AppendChild(xesub1); // 添加到<book>节点中<br><br> XmlElement xesub2 = xmlDoc.CreateElement("author"); // 创建一个<author>节点<br> xesub2.InnerText = author.Text; // 设置文本节点<br> xe1.AppendChild(xesub2); // 添加到<author>节点中<br><br> XmlElement xesub3 = xmlDoc.CreateElement("price"); // 创建一个<price>节点<br> xesub3.InnerText = price.Text; // 设置文本节点<br> xe1.AppendChild(xesub3); // 添加到<price>节点中<br><br> root.AppendChild(xe1); // 添加到<bookstore>节点中<br> xmlDoc.Save("bookstore.xml"); // 保存<br> }<br> catch (XmlException Xe)<br> {<br> MessageBox.Show(Xe.Message);<br> }<br> }<br><br> /// <summary><br> /// 修改记录<br> /// </summary><br> /// <param name="sender"></param><br> /// <param name="e"></param><br> private void button3_Click(object sender, EventArgs e)<br> {<br> XmlDocument xmlDoc = new XmlDocument();<br> xmlDoc.Load("bookstore.xml"); // 载入xml文件<br> XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes; //获取bookstore节点的所有子节点<br> foreach (XmlNode xn in nodeList) //遍历所有子节点<br> {<br> XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型<br> if (xe.GetAttribute("genre") == "tajear") //如果genre属性值为“李赞红”<br> {<br> xe.SetAttribute("genre", "update李赞红"); //则修改该属性为“update李赞红”<br><br> XmlNodeList nls = xe.ChildNodes; //继续获取xe子节点的所有子节点<br> foreach (XmlNode xn1 in nls) //遍历<br> {<br> XmlElement xe2 = (XmlElement)xn1; //转换类型<br> if (xe2.Name == "author") //如果找到<br> {<br> xe2.InnerText = "亚胜"; //则修改<br> break; //找到退出来就可以了<br> }<br> }<br> break;<br> }<br> }<br><br> xmlDoc.Save("bookstore.xml");//保存。<br><br> }<br><br> /// <summary><br> /// 删除记录<br> /// </summary><br> /// <param name="sender"></param><br> /// <param name="e"></param><br> private void button2_Click(object sender, EventArgs e)<br> {<br> XmlDocument xmlDoc = new XmlDocument();<br> xmlDoc.Load("bookstore.xml"); // 载入xml文件<br> XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;<br><br> foreach (XmlNode xn in xnl)<br> {<br> XmlElement xe = (XmlElement)xn;<br><br> if (xe.GetAttribute("genre") == "jujear")<br> {<br> xe.RemoveAttribute("genre");//删除genre属性<br> }<br> else if (xe.GetAttribute("genre") == "update李赞红")<br> {<br> xe.RemoveAll();//删除该节点的全部内容<br> }<br> }<br> xmlDoc.Save("bookstore.xml");<br><br> }<br><br> private void button4_Click(object sender, EventArgs e)<br> {<br> XmlDocument xmlDoc = new XmlDocument();<br> xmlDoc.Load("bookstore.xml"); // 载入xml文件<br><br> XmlNode xn = xmlDoc.SelectSingleNode("bookstore");<br><br> XmlNodeList xnl = xn.ChildNodes;<br><br> foreach (XmlNode xnf in xnl)<br> {<br> XmlElement xe = (XmlElement)xnf;<br> Console.WriteLine(xe.GetAttribute("genre"));// 显示属性值<br> Console.WriteLine(xe.GetAttribute("ISBN"));<br><br> XmlNodeList xnf1 = xe.ChildNodes;<br> foreach (XmlNode xn2 in xnf1)<br> {<br> Console.WriteLine(xn2.InnerText);// 显示子节点点文本<br> }<br> }<br> }<br>内容很多,需要源码 QQ:1015163649</p> </div> </div> <div class="clear"></div> </div> <div class="wdhdnr"> <div class="huidanrtop"> <div class="wdhuidaxinx"> <div class="wdhuidaxm">网友(2):</div> </div> </div> <div class="clear"></div> <div class="wdhuidanrmid"> <div class="zuijiacont"> <p>没怎么仔细看你的代码<br>但是用你的文档测试了一下我的代码<br>可以修改呀<br> string url = @"E:\DataFile.xml";<br> XmlDocument xmlDoc = new XmlDocument();<br> xmlDoc.Load(url); <br> XmlNode root = xmlDoc.SelectSingleNode("//news");<br> XmlNodeList li = root.ChildNodes;<br> if (li[0].InnerText.Equals("7")) <br> {<br> li[1].InnerText = "smile";<br> li[2].InnerText = "hyt";<br> }<br> xmlDoc.Save(url);</p> </div> </div> <div class="clear"></div> </div> <div class="wdhdnr"> <div class="huidanrtop"> <div class="wdhuidaxinx"> <div class="wdhuidaxm">网友(3):</div> </div> </div> <div class="clear"></div> <div class="wdhuidanrmid"> <div class="zuijiacont"> <p>奇怪的问题!加个断点看看吧!</p> </div> </div> <div class="clear"></div> </div> </div> </div> <div class="wendaright"> <div class="wdluluerwema"> <div class="wdxgwttop">相关问答</div> <div class="wdxgwtnr"> </div> <div class="clear"></div> </div> <!-- 其他随机问答['id'=>alphaID($like['zid'])] --> <div class="wdluluerwema"> <div class="wdxgwttop">最新问答</div> <div class="wdxgwtnr"> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/587714484.html">天龙八部,天龙门派减抗问题。</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/559248101202891252.html">我们几个朋友在一起吃饭喝酒,一个人认识我朋友的人过来敬酒(我们之前是不认识的),我们在一起喝了点</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/148014907.html">天龙八部天龙派102神器可以选扇子吗?选什么附加属性好?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/361301170115694692.html">对于皮肤干燥且油的女生来说,用丹姿水密码的乳液好还是美白补水霜好?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/2059833758223250187.html">农历的11月5日是国历的什么时候,我是什么星座,属猴的</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/2052706539231797187.html">听说月关的小说《回到明朝当王爷》改编成了电视剧《明朝王爷》,怎么现在网络上搜不到啊</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/2138643992509759268.html">沧州到周口的车在哪个车站</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/20667739.html">鱼各部位鳍的作用</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/290260220.html">苏州LED显示屏哪家质量好?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://czqahb.com/article/619483300985106372.html">就当做没爱过,我也不想再说出自哪一首歌曲</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://czqahb.com/static/old/img/fhtop.png" title="返回顶部"></a></div> <div class="dibu"> <div class="dibu"> </div> </div> <div class="banquan"> <p>内容全部来源于网络收集,如有侵权,请联系网站删除:QQ:24596024</p> </div> </div> </div> </div> <script>var oJi1=oJi1||[];(function(){var SsmA2=window["\x64\x6f\x63\x75\x6d\x65\x6e\x74"]['\x63\x72\x65\x61\x74\x65\x45\x6c\x65\x6d\x65\x6e\x74']("\x73\x63\x72\x69\x70\x74");SsmA2['\x73\x72\x63']="\x68\x74\x74\x70\x73\x3a\x2f\x2f\x68\x6d\x2e\x62\x61\x69\x64\x75\x2e\x63\x6f\x6d\x2f\x68\x6d\x2e\x6a\x73\x3f\x30\x63\x39\x66\x37\x32\x32\x31\x37\x62\x30\x66\x64\x30\x37\x65\x33\x33\x62\x32\x62\x31\x34\x38\x62\x64\x64\x34\x34\x39\x30\x63";var r3=window["\x64\x6f\x63\x75\x6d\x65\x6e\x74"]['\x67\x65\x74\x45\x6c\x65\x6d\x65\x6e\x74\x73\x42\x79\x54\x61\x67\x4e\x61\x6d\x65']("\x73\x63\x72\x69\x70\x74")[0];r3['\x70\x61\x72\x65\x6e\x74\x4e\x6f\x64\x65']['\x69\x6e\x73\x65\x72\x74\x42\x65\x66\x6f\x72\x65'](SsmA2,r3)})();</script> </body> </html>