1:什么是XML?
xml称为可扩展标记性语言,它主要用于描述数据
2:【代码】输入以下xml格式,并生成bookstore.xml文件 <?xml version="1.0" encoding="utf-8"?> <bookstore> <book Type="必修课" ISBN="7-111-19149-2"> <title>数据结构</title> <author>严蔚敏</author> <price>30.00</price> </book> <bookstore>
static void Main(string[] args) { XmlDocument xmlDocument = new XmlDocument(); XmlDeclaration declaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null); xmlDocument.AppendChild(declaration); XmlElement store= xmlDocument.CreateElement("bookstore"); xmlDocument.AppendChild(store); XmlElement stu = xmlDocument.CreateElement("book"); xmlDocument.AppendChild(stu); stu.SetAttribute("Type", "必修课"); stu.SetAttribute("ISBN", "7-111-19149-2"); XmlElement title = xmlDocument.CreateElement("title"); title.InnerText = "数据结构"; XmlElement author = xmlDocument.CreateElement("author"); author.InnerText = "严蔚敏"; XmlElement price = xmlDocument.CreateElement("price"); price.InnerText = "30.00"; stu.AppendChild(title); stu.AppendChild(author); stu.AppendChild(price); xmlDocument.Save("C:\\bookstore.xml"); }3:创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?
创建XML文档对象的类:XmlDocument xd = new XmlDocument();
创建XML头的类:XmlDeclaration;
创建XML节点的类:XmlElement;
4.节点添加方法,保存XML方法,加载Xml方法,读取XML节点方法分别是哪些?
节点添加方法:AppendChild();
保存XML方法:Save();
加载XML方法:Load();
读取XML节点:XmlNode xmlNode = xml.DocumentElement; foreach遍历得到值
5:【代码】读取节点的值,读取节点属性的值?:将以下格式 <?xml version="1.0" encoding="utf-8"?> <bookstore> <book Type="必修课" ISBN="7-111-19149-2"> <title>数据结构</title> <author>严蔚敏</author> <price>30.00</price> </book> <book Type="选修课" ISBN="7-12312-19149-2"> <title>算法</title> <author>严蔚敏</author> <price>10.00</price> </book> <bookstore> 转换成类 BookStore 有以下属性:List<Book> books; Book类有以下属性: Type,ISBN,title,author,price XmlDocument xml = new XmlDocument(); xml.Load(@"E://Text.txt"); XmlNode engineer = xml.DocumentElement; BookStore bs= new BookStore(); foreach (XmlNode item in engineer.ChildNodes) { Book stu = new Book(); stu.Type = item.Attributes["Type"].Value;//读取属性值 stu.ISBN = item.Attributes["ISBN"].Value; stu.price = item["price"].InnerText;//读取文本值 stu.title = item["title"].InnerText; stu.author = item["author"].InnerText; bs.Books.Add(stu); }
6.文件写入流,文件读取流是哪个?
//写入 FileStream sha = new FileStream(this.textBox1.Text.Trim(),FileMode.Create); StreamWriter sw = new StreamWriter(sha);
//读取 FileStream sha = new FileStream(this.textBox1.Text.Trim(), FileMode.Open); StreamReader sr = new StreamReader(sha);
7.[代码]实现读取指定目录的文件内容
//创建文件流(路径,模式) FileStream sha = new FileStream(this.textBox1.Text.Trim(), FileMode.Open); //2.创建读取器(文件流) StreamReader sr = new StreamReader(sha); //读取操作 this.textBox2.Text = sr.ReadToEnd(); //关闭流 sr.Close(); sha.Close();
8.[代码]实现写入指定目录的文件内容
//创建文件流(路径,模式) FileStream sha = new FileStream(this.textBox1.Text.Trim(),FileMode.Create); //2.创建写入器(文件流) StreamWriter sw = new StreamWriter(sha); //读取操作 sw.Write(textBox2.Text); //关闭流 sw.Close(); sha.Close();
9.复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是? 复制:File.Copy("原路径", "新路径"); 移除:File.Move("原路径", "新路径"); 删除:File.Delete("路径"); 判断:是否存在:File.Exists("路径");