- usingSystem;
- usingSystem.Windows.Forms;
- usingSystem.Xml;
- namespaceXMLDemo
- {
- classFrmDOM:Form
- publicFrmDOM()
- InitializeComponent();
- }
- {
- newXmlDocument();
- xmlDoc.Load("Books.xml");
- MessageBox.Show(xmlDoc.InnerXml);
- }
- //xml文档
- null);
- xmlDoc.AppendChild(dec);
- //创建根节点
- XmlElementroot=xmlDoc.CreateElement("Books");
- xmlDoc.AppendChild(root);
- //节点及元素
- XmlNodebook=xmlDoc.CreateElement("Book");
- "WindowForm");
- "111111");
- "amandag");
- "128.00");
- "¥");
- book.AppendChild(title);
- book.AppendChild(isbn);
- book.AppendChild(author);
- book.AppendChild(price);
- root.AppendChild(book);
- xmlDoc.Save("Books.xml");
- MessageBox.Show("数据已写入!");
- XmlNoderoot=xmlDoc.SelectSingleNode("Books");
- XmlElementbook=xmlDoc.CreateElement("Book");
- "ASP.NET");
- "222222");
- "moon");
- "111.00");
- MessageBox.Show("数据已插入!");
- //方法1:获取Books//Book节点的第一个子节点
- XmlNodeListnodeList=xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
- null;
- //遍历所有子节点
- innodeList)
- //将子节点类型转换为XmlElement类型
- xe=(XmlElement)xn;
- if(xe.Name=="Author"&&xe.InnerText=="amandag")
- xe.InnerText="高歌";
- if(xe.GetAttribute("Unit")=="¥")
- //方法2:
- XmlNodenode=xmlDoc.SelectSingleNode("Books//Book[Author=\"moon\"]//Author");
- null)
- node.InnerText="宝贝";
- xmlDoc.Save("Books.xml");
- MessageBox.Show("数据已更新!");
- newXmlDocument();
- xmlDoc.Load("Books.xml");
- XmlNodeListnodeList=xmlDoc.SelectNodes("Books//Book//Price[@Unit=\"$\"]");
- XmlElementxe=(XmlElement)xn;
- xe.RemoveAttribute("Unit");
- MessageBox.Show("数据已删除!");
- stringvalue)
- XmlElementelement=doc.CreateElement(elementName);
- element.InnerText=value;
- returnelement;
- }
原文地址:https://www.jb51.cc/xml/299063.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。