1:什么是XML?
XML称为可扩展标记性语言,是eXtensible Markup Language的缩写。
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>
public static void addInnerText(string str)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration xd = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(xd);
XmlElement bookstore = doc.CreateElement("bookstore");
doc.AppendChild(bookstore);
XmlElement book = doc.CreateElement("book");
bookstore.AppendChild(book);
book.SetAttribute("Type", "必修课");
book.SetAttribute("ISBN", "7-111-19149-2");
XmlElement title = doc.CreateElement("title");
book.AppendChild(title);
title.InnerText = "数据结构";
XmlElement author = doc.CreateElement("author");
book.AppendChild(author);
author.InnerText = "严蔚敏";
XmlElement price = doc.CreateElement("price");
book.AppendChild(price);
price.InnerText = "30.00";
doc.Save(str);
}
3:创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?
XML文档对象:XmlDocument;
XML头:XmlDeclaration;
XML节点:XmlElement;
4:节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是?
添加:AppendChild();
保存:Save();
加载:Load();
读取:InnerText
5:【代码】读取节点的值,读取节点属性的值?
节点值:node["节点名"].innerText;
属性值:Attributes["type"].Value;
06:将以下格式
<?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
BookStore store = new BookStore();
store.books = new List<Book>();
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\\txt.xml");
XmlNode node = doc.SelectSingleNode("bookstore");
foreach (XmlNode item in node.ChildNodes)
{
Book book = new Book();
book.Type = item.Attributes["Type"].Value;
book.ISBN = item.Attributes["ISBN"].Value;
book.title = item["title"].InnerText;
book.author = item["author"].InnerText;
book.price = Convert.ToDouble(item["price"].InnerText);
store.books.Add(book);
}
6:文件写入流,文件读取流是哪个?
写入流:StreamWriter;
读取流:StreamReader;
7:【代码】实现读取指定目录的文件内容
FileStream myfs=new FileStream ("地址",FileMode.Open);
StreamReader mysr=new StreamReader (myfs);
mysr.ReadToend();
mysr.Close();
myfs.Close();
8:【代码】实现写入指定目录的文件内容
FileStream myfs=new FileStream ("地址",FileMode.Create);
StreamWriter mysw=new StreamReader (myfs);
mysw.Write("内容");
mysw.Close();
myfs.Close();
9:复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
复制文件:Copy();
移动文件:Move();
删除文件:Delete();
判断文件是否存在:Exists();
读取指定目录下的所有目录的方法:GetFiles();