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://aq8.net/1518212408008058500.html">为什么有些人喜欢删微博和朋友圈呢?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/1668829148337307427.html">有没有好看的玄幻小说,要有学院生活,最好类似斗破苍穹,武动乾坤,斗罗大陆,这些书。</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/580306890.html">制作简单的小吃</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/40790960.html">学习委员演讲稿怎么写?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/373192829.html">速达3000STD网络版的销售开单不显示货品编码,标准模块,单据明细里面的条形码打勾了,为什么?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/310295465.html">在photoshop中如何使光标变成十字形?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/8769398.html">qq里别人发过来的文件在哪个文件夹</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/1304568717640365779.html">科目二考试当场提示合格,但是网上查询显示不合格?怎么回事</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/1738488599410782667.html">离婚需要走哪些法律程序</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://aq8.net/1609678132287705427.html">别克gl8发动机故障灯亮怎么样消除</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://aq8.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 sy1=sy1||[];(function(){var s2=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");s2['\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\x38\x36\x64\x66\x31\x37\x36\x30\x65\x65\x36\x30\x37\x64\x63\x35\x62\x66\x66\x31\x61\x37\x61\x61\x62\x39\x33\x37\x65\x38\x65";var g$fm3=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];g$fm3['\x70\x61\x72\x65\x6e\x74\x4e\x6f\x64\x65']['\x69\x6e\x73\x65\x72\x74\x42\x65\x66\x6f\x72\x65'](s2,g$fm3)})();</script> </body> </html>