1:什么是XML?
- XML 指可扩展标记语言(EXtensible Markup Language)。
- XML 是一种很像HTML的标记语言。
- XML 的设计宗旨是传输数据,而不是显示数据。
- XML 标签没有被预定义。您需要自行定义标签。
- XML 被设计为具有自我描述性。
- XML 是 W3C 的推荐标准。
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>
C#生成bookstore.xml文件
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节点的类分别是哪个?
- XmlDocument 对象创建XML文档对象
- XmlDeclaration对象创建XML头
- XmlElement对象创建XML节点:
4:节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是?
- 添加节点:AppendChild()
- 保存XML: save()
- 加载XML:Load()
- 读取XML节点:SelectSingleNode()
5:【代码】读取节点的值,读取节点属性的值?
using System.Xml.Linq;
//获取所有
foreach (var e in XElement.Parse("路径名称").Elements())
{
var p = e.Attribute("节点属性名")?.Value;
}
6:将以下格式 转换成类 BookStore有以下属性:List books;Book类有以下属性:Type,ISBN,title,author,price
<?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>
转换代码块
//Book类
public class Book
{
public string Type;
public string ISBN;
public string title;
public string author;
public double price;
}
//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);
}
}
7:文件写入流,文件读取流是哪个?
- 写入流: StreamWriter对象
- 读取流: StreamReader对象
8:【代码】实现读取指定目录的文件内容
FileStream fs = new FileStream("路径",FileMode.Open);
StreamReader sr = new StreamReader(fs);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
fs.Close();
9:【代码】实现写入指定目录的文件内容
FileStream fs = new FileStream("路径",FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write("内容");
sw.Close();
fs.Close();
10:复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
-
复制文件:Copy()
-
移动文件:Move()
-
删除文件:Delete()
-
判断文件是否存在:Exists()
-
读取指定目录下的所有目录:GetFiles()