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 doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(declaration);
XmlElement bookstore = doc.CreateElement("bookstore");
doc.AppendChild(bookstore);
XmlElement book = doc.CreateElement("book");
book.SetAttribute("Type", "必修课");
book.SetAttribute("ISBN", "7-111-19149-2");
bookstore.AppendChild(book);
XmlElement title = doc.CreateElement("title");
title.InnerText = "数据结构";
book.AppendChild(title);
XmlElement author = doc.CreateElement("author");
author.InnerText = "严蔚敏";
book.AppendChild(author);
XmlElement price = doc.CreateElement("price");
price.InnerText = "30.00";
book.AppendChild(price);
doc.Save(@"d:\\str.xml");
3.创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?
答:
创建XML文档对象 XmlDocument
创建XML头的类 XmlDeclaration
创建XML节点的类 XmlElement
4.节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是?
答:
节点添加 AppendChild()
保存XML Save()
加载XML Load()
读取XML节点
XmlNode node = doc.SelectSingleNode("xxx");
foreach (XmlNode item in node.ChildNodes) { }
5.读取节点的值,读取节点属性的值?
答:
XmlNode node = doc.SelectSingleNode("xxx");//获取指定二层节点
foreach (XmlNode item in node.ChildNodes) //在获取三层节点
{
book.Type = item.Attributes["Type"].Value;//读取节点属性的值
book.ISBN = item.Attributes["ISBN"].Value;//读取节点属性的值
book.title = item["title"].InnerText; //读取节点的值
book.author = item["author"].InnerText; //读取节点的值
}
6:将以下格式
<?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(@"D:\\str.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);
}
foreach (Book item in store.books)
{
Console.WriteLine(item.Type+"\t"+item.ISBN+"\t"+item.title+"\t"+item.price+"\t"+item.author);
7.文件写入流,文件读取流是哪个?
答:
文件写入流 StreamWriter
文件读取流 StreamReader
8.实现读取指定目录的文件内容
答:
public string show(string path)
{
if(!File.Exists())//文件是否存在
{
return;
}
FileStream fs = new FileStream(filePath,FileMode.Opne);
StreamReader sr= new StreamReader(fs,Encoding.Default);
string str = sr.ReadToEnd()
sr.Close();
fs.Close();
return str;
}
9.实现写入指定目录的文件内容
答:
public void SetText(string path,string str)
{
FileStream fs = new FileStream(path,FileMode.Append);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
sw.Write(str);
sw.Close();
fs.Close();
}
10.复制文件,移动文件,删除文件,判断文件是否存在
答:复制 File.Copy(原文件位置,复制到的位置 );
移动 File.Move();
删除 File.Delete();
判断文件是否存在 File.Exists();