什么是XML
XML称为可扩展标记性语言,是extensible Markup Language的缩写。在.NET框架中XML是非常重要的一部分,它用于描述数据,是当前处理结构化文档信息的有力工具。
2输入以下xml格式,并生成bookstore.xml文件
<bookstore>
<book Type="必修课" ISBN="7-111-19149-2">
<title>数据结构</title>
<author>严蔚敏</author>
<price>30.00</price>
</book>
<bookstore>
XmlDocument doc = new XmlDocument();
XmlDeclaration dec= doc.CreateXmlDeclaration("1.0","utf-8",null);
doc.AppendChild(dec);
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("E://bookstore.xml");
3创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个
1.创建XML文档对象:XmlDocument 对象
2.创建XML头:XmlDeclaration对象
3.创建XML节点: XmlElement对象
4节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是
1.节点添加方法:AppendChild()
2.保存XML方法: save()
3.加载XML方法:Load()
4.读取XML节点方法:SelectSingleNode()
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>
public class BookStore
{
public List<Book> Books { get; set; }
}
static void Main(string[] args)
{
BookStore bs = new BookStore();
bs.Books = new List<Book>();
XmlDocument doc = new XmlDocument();
doc.Load("E://bookstore.xml");
XmlNode root = doc.SelectSingleNode("bookstore");
foreach (XmlNode node in root.ChildNodes)
{
Book book = new Book();
book.Type = node.Attributes["Type"].Value;
book.ISBN = node.Attributes["ISBN"].Value;
book.title = node["title"].InnerText;
book.author = node["author"].InnerText;
book.price = Convert.ToDouble(node["price"].InnerText);
bs.Books.Add(book);
}
foreach (Book item in bs.Books)
{
Console.WriteLine(item.Type + "\t" + item.ISBN + "\t" + item.title + "\t" + item.price + "\t" + item.author);
}
Console.ReadLine();
}
6文件写入流,文件读取流是哪个
写入流: StreamWriter对象
读取流: StreamReader对象
7实现读取指定目录的文件内容
FileStream fs = new FileStream("E://text.xml",FileMode.Open);
StreamReader sr = new StreamReader(fs);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
fs.Close();
8实现写入指定目录的文件内容
FileStream fs = new FileStream("E://text.xml",FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write("1234567890");
sw.Close();
fs.Close();
9复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是
复制文件:Copy()
移动文件:Move()
删除文件:Delete()
判断文件是否存在:Exists()
读取指定目录下的所有目录:GetFiles()