插入1.txt

来自「C# 是创新性的新式编程语言」· 文本 代码 · 共 35 行

TXT
35
字号
using System.Data;
using System.Xml;
using System.IO;
private XmlDocument doc;			
//插入节点
			doc.Load("books.xml");
			XmlElement newBook=doc.CreateElement("book");
			newBook.SetAttribute("genre","Mystery");
			newBook.SetAttribute("publicationdate","2001");
			newBook.SetAttribute("ISBN","123456789");
			//
			XmlElement newTitle=doc.CreateElement("title");
			newTitle.InnerText="Case of the Missing Cookie";
			newBook.AppendChild(newTitle);
			//
			XmlElement newAuthor=doc.CreateElement("author");
			newBook.AppendChild(newAuthor);
			//
			XmlElement newName=doc.CreateElement("name");
			newName.InnerText="C.Monster";
			newAuthor.AppendChild(newName);
			//
			XmlElement newPrice=doc.CreateElement("price");
			newPrice.InnerText="9.95";
			newBook.AppendChild(newPrice);
			//
			doc.DocumentElement.AppendChild(newBook);
			//
			XmlTextWriter tr=new XmlTextWriter("booksedit.xml",null);
			tr.Formatting=Formatting.Indented;
			doc.WriteContentTo(tr);
			tr.Close();
			XmlNodeList nodeLst=doc.GetElementsByTagName("title");
			foreach(XmlNode node in nodeLst)
				listBox1.Items.Add(node.InnerText);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?