xml是一种标签语言,常用于存储处理数据。在Csharp中创建xml文档的方式如下:
首先引入命名空间
using System.Xml;
然后创建文档并给文档添加基本信息和节点信息:
XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); XmlElement books = doc.CreateElement("Books"); doc.AppendChild(books); XmlElement book1 = doc.CreateElement("Book"); books.AppendChild(book1); XmlElement bookName1 = doc.CreateElement("BookName"); bookName1.InnerText = "水浒传"; book1.AppendChild(bookName1); XmlElement author1 = doc.CreateElement("Author"); author1.InnerXml = "<authorName>吴承恩</authorName>"; book1.AppendChild(author1); XmlElement price1 = doc.CreateElement("Price"); price1.InnerXml = "100¥"; book1.AppendChild(price1); XmlElement des1 = doc.CreateElement("Des"); des1.InnerXml = "好看,顶!~!!!!"; book1.AppendChild(des1); Console.WriteLine("保存成功"); doc.Save("Book.xml"); Console.ReadKey();