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)
{
string path = @"C:\\bookstore.xml";
FileStream fileStream = new FileStream(path,FileMode.Create);
XmlDocument xml = new XmlDocument();
XmlDeclaration xmlDeclaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);
xml.AppendChild(xmlDeclaration);
XmlElement xmlElement = xml.CreateElement("bookstore");
xml.AppendChild(xmlElement);
XmlElement xmlElement1 = xml.CreateElement("book");
xmlElement1.SetAttribute("Type", "必修课");
xmlElement1.SetAttribute("ISBN", "7-111-19149-2");
xmlElement.AppendChild(xmlElement1);
XmlElement xmlElement2 = xml.CreateElement("title");
xmlElement2.InnerText = "数据结构";
xmlElement1.AppendChild(xmlElement2);
XmlElement xmlElement3 = xml.CreateElement("author");
xmlElement3.InnerText = "严蔚敏";
xmlElement1.AppendChild(xmlElement3);
XmlElement xmlElement4 = xml.CreateElement("price");
xmlElement4.InnerText = "30.00";
xmlElement1.AppendChild(xmlElement4);
fileStream.Close();
xml.Save(path);
}
3:创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?
XmlDocument xml = new XmlDocument(); //XML文档对象
XmlDeclaration xmlDeclaration = xml.CreateXmlDeclaration("1.0", "utf-8", null); //,创建XML头
xml.AppendChild(xmlDeclaration);
XmlElement xmlElement = xml.CreateElement("bookstore"); //创建XML节点
4:节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是?
XmlDocument xml = new XmlDocument(); //XML文档对象
xml.AppendChild(); //节点添加方法
xml.Save(path); //保存XML方法
xml.Load(path); //加载XML方法
xml.SelectNodes(); //读取XML节点
5:【代码】读取节点的值,读取节点属性的值?
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);
}
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(@"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);
}
6:文件写入流,文件读取流是哪个?
string path=@"C://text.txt";
FileStream fileStream=new FileStream(path,FileMode.Create); //文件写入流
string path=@"C://text.txt";
FileStream fileStream=new FileStream(path,FileMode.Create);
StreamWriter streamWriter=new StreamWriter(fileStream); //文件读取流
7:【代码】实现读取指定目录的文件内容
static void Main(string[] args)
{
FileStream file = null;
StreamReader streamReader = null;
try
{
string path = @"C:\\text.txt";
file = new FileStream(path, FileMode.Open);
streamReader = new StreamReader(file, Encoding.Default);
string str = streamReader.ReadLine();
Console.WriteLine(str);
Console.ReadKey();
}
catch (Exception)
{
throw;
}
finally
{
file.Close();
streamReader.Close();
}
}
8:【代码】实现写入指定目录的文件内容
static void Main(string[] args)
{
FileStream fileStream = null;
StreamWriter streamWriter = null;
try
{
string path = @"C:\\text.txt";
fileStream = new FileStream(path, FileMode.Create);
streamWriter = new StreamWriter(fileStream);
string str = "HelloWorld";
streamWriter.Write(str);
}
catch (Exception)
{
throw;
}
finally
{
streamWriter.Close();
fileStream.Close();
}
}
9:复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
Copy()//复制文件
Delete()//移动文件
Move()//删除文件
Exists()//判断文件是否存在
GetDirectories()//返回目录