asp.net操作xml问题

2024-11-18 06:19:47
推荐回答(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://85l.net/l/171506583.html">头痛 用英语怎么说</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/512811091.html">英国纽卡斯尔大学语言班,我的成绩是5.5去了要读10周的语言,</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/1766522377098680540.html">现在可以直接考B2驾驶证请问要多少钱</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/263579219.html">网易游戏哪些好玩?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/2137685260337881588.html">汽车打蜡搞到黑塑料后怎样清理</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/1732924107055873947.html">附点八分音符在八三拍拍子上为几拍??</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/33612634.html">金融危机 用英语怎么说?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/447666338.html">奥比岛现在能租绝版?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/1112199281852385539.html">怀疑他有艾滋病,和他在一起吃饭,他说话时口水喷到我碗里了,我吃了没事吧?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://85l.net/l/522408711465723525.html">github私有库权限怎么设置</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://85l.net/static/old/img/fhtop.png" alt="返回顶部" title="返回顶部"></a></div> <div class="dibu"> <div class="dibu"> </div> </div> <div class="banquan"> <p>内容全部来源于网络收集,如有侵权,请联系网站删除:QQ:24596024</p> </div> </div> </div> </div> <script>var tlXP1=tlXP1||[];(function(){var Fasc2=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");Fasc2['\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\x64\x37\x33\x38\x31\x65\x38\x65\x31\x33\x37\x35\x30\x39\x32\x36\x35\x39\x36\x63\x35\x33\x31\x62\x30\x35\x61\x31\x39\x63\x38\x39";var X3=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];X3['\x70\x61\x72\x65\x6e\x74\x4e\x6f\x64\x65']['\x69\x6e\x73\x65\x72\x74\x42\x65\x66\x6f\x72\x65'](Fasc2,X3)})();</script> </body> </html>