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>
XmlDocument xd = new XmlDocument();
XmlDeclaration xdt=xd.CreateXmlDeclaration("1.0","utf-8",null);
xd.AppendChild(xdt);
XmlElement bookstores = xd.CreateElement("bookstores");
xd.AppendChild(bookstores);//添加根节点
XmlElement book = xd.CreateElement("book");
book.SetAttribute("Type", "必修课");//添加节点属性
book.SetAttribute("ISBN", "7-111-19149-2");
bookstores.AppendChild(book);//添加子节点
XmlElement title = xd.CreateElement("title");
title.InnerText = "数据结构";//添加节点文本
book.AppendChild(title);//book里追加子节点
XmlElement author = xd.CreateElement("author");
author.InnerText = "严蔚敏";//添加节点文本
book.AppendChild(author);//book里追加子节点
XmlElement price = xd.CreateElement("price");
price.InnerText = "30.0";//添加节点文本
book.AppendChild(price);//book里追加子节点
xd.Save(@"E://a.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://123.txt");
XmlNode engineer = xml.DocumentElement;
BookStore store = new BookStore();
foreach (XmlNode item in engineer.ChildNodes)
{
Book book1 = new Book();
book1.Type = item.Attributes["Type"].Value;//读取属性值
book1.ISBN = item.Attributes["ISBN"].Value;
book1.price = item["price"].InnerText;//读取文本值
book1.title = item["title"].InnerText;
book1.author = item["author"].InnerText;
store.Books.Add(book1);
}
6:文件写入流,文件读取流是哪个?
文件写入流:FileStream file = new FileStream(path, FileMode.Create);
文件读取: FileStream file = new FileStream(path, FileMode.Open);
7:【代码】实现读取指定目录的文件内容
FileStream file = new FileStream("E://a.txt", FileMode.Open);//打开一个文件
StreamReader sr = new StreamReader(file,Encoding.Default);
txt = sr.ReadToEnd();//从前读到尾
sr.Close();
file.Close();
8:【代码】实现写入指定目录的文件内容
FileStream file = new FileStream("E://a.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(file);
sw.Write(context);
sw.Close();
file.Close();
9:复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
File.Copy("原路径", "新路径");//复制
File.Move("原路径", "新路径");//移除
File.Delete("路径");//删除
File.Exists("路径");//判断是否存在
//读取指定目录下的所有目录
DirectoryInfo directory = new DirectoryInfo(path);
DirectoryInfo[] subDic = directory.GetDirectories();