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>
public class Program
{
public void Err(string path)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(declaration);
XmlElement xmlElement = doc.CreateElement("bookstore");
doc.AppendChild(xmlElement);
XmlElement book = doc.CreateElement("book");
book.SetAttribute("Type", "必修课");
book.SetAttribute("ISBN", "7-111-19149-2");
book.InnerText = null;
xmlElement.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(path);
}
static void Main(string[] args)
{
Program xm = new Program();
string path = "D:\\bookstore.xml";
xm.Err(path);
}
}
3:创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?
创建XML文档对象的类是:XmlDocument;
创建XML头的类是: XmlDeclaration;
创建XML节点是: XmlElement 。
4:节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是?
添加节点:xmlDoc;
保存xml方法:save();
加载xml的方法:load();
读取xml节点的办法:xmlNode。
5:【代码】读取节点的值,读取节点属性的值?
读取节点的值(注意:以下代码只是我从我指定的xml里的节点值)
static void Main(string[] args)
{
XmlDocument myml = new XmlDocument();
myml.Load("D:\\Test.xml");
XmlElement xmlNode = myml.DocumentElement;
foreach (XmlNode node in xmlNode.ChildNodes)
{
Console.WriteLine(node["title"].InnerText);
}
Console.ReadLine();
}
读取节点属性的值
static void Main(string[] args)
{
string xmls = null;
XmlDocument myml = new XmlDocument();
myml.Load("D:\\Test.xml");
XmlElement xmlNode = myml.DocumentElement;
foreach (XmlNode node in xmlNode.ChildNodes)
{
xmls = node.Attributes["Type"].Value;
Console.WriteLine(xmls);
}
Console.ReadLine();
}
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 books;
Book类有以下属性:
Type,ISBN,title,author,price
static void Main(string[] args)
{
BookStore store = new BookStore();
store.books = new List<Book>();
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\\Test.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);
}
Console.ReadLine();
}
7:文件写入流,文件读取流是哪个?
写入流:StreamWriter;
文件读取流:StreamReader
8:【代码】实现读取指定目录的文件内容
static void Main(string[] args)
{
string conser = null;
FileStream myfs = new FileStream("D:\\wangyong.txt", FileMode.Open);
StreamReader mysr = new StreamReader(myfs,Encoding.UTF8);
conser= mysr.ReadToEnd();
Console.WriteLine(conser);
mysr.Close();
myfs.Close();
Console.ReadLine();
}
9:实现写入指定目录的文件内容
static void Main(string[] args)
{
FileStream myfs = new FileStream("D:\\wangyong.txt", FileMode.Open);
StreamWriter mysw = new StreamWriter(myfs);
mysw.Write("啊啊啊");
mysw.Close();
myfs.Close();
Console.ReadLine()
}
10:复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
复制文件:copy(string sourceFillePath,string destinationFilePath)
删除文件:Delete(string path)
判断文件是否存在:Exists(string path)
读取指定目录下的所有目录:FullName