1.什么是XML
xml称为可扩展标记性语言,可以实现不同系统之间的数据交互
【代码】输入以下xml格式,并生成bookstore.xml文件
<bookstore>
<book Type="必修课" ISBN="7-111-19149-2">
<title>数据结构</title>
<author>严蔚敏</author>
<price>30.00</price>
</book>
<bookstore>
public static void Writed() {
XmlDocument xmlDocument = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration(“1.0”,“utf-8”,null);
XmlElement xmlElement = xmlDocument.CreateElement(“bookstore”);
XmlElement xmlElement1 = xmlDocument.CreateElement(“book”);
XmlElement xmlElement2 = xmlDocument.CreateElement(“title”);
XmlElement xmlElement3 = xmlDocument.CreateElement(“author”);
XmlElement xmlElement4 = xmlDocument.CreateElement(“price”);
xmlElement1.SetAttribute(“Type”, “必修课”);
xmlElement1.SetAttribute(“ISBN”, “7-111-19149-2”);
xmlElement2.InnerText = “数据结构”;
xmlElement3.InnerText = “严蔚敏”;
xmlElement4.InnerText = “30.00”;
xmlDocument.AppendChild(xmlElement);
xmlElement.AppendChild(xmlElement1);
xmlElement1.AppendChild(xmlElement2);
xmlElement1.AppendChild(xmlElement3);
xmlElement1.AppendChild(xmlElement3);
xmlElement1.AppendChild(xmlElement4);
xmlDocument.Save(“F:\Test3.xml”);
}
3:创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?
public static XmlDocument xmlDocument;
public static void CreateXMLElement(string path) {
xmlDocument = new XmlDocument();
}
public static void Head(string path) {
CreateXMLElement( path);
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration(“1.0”,“utf-8”,null);
}
public static void AppendNode(string path,string name) {
CreateXMLElement( path);
XmlElement xmlElement = xmlDocument.CreateElement(name);
xmlDocument.AppendChild(xmlElement);
}
4:节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是?
public static XmlDocument xmlDocument;
public static void AppendNode(string path,string name) {
CreateXMLElement( path);
XmlElement xmlElement = xmlDocument.CreateElement(name);
xmlDocument.AppendChild(xmlElement);
}
public static void Save(string path)
{
xmlDocument.Save(path);
}
public static void Load(string path)
{
xmlDocument.Load(path);
}
5:【代码】读取节点的值,读取节点属性的值?
ublic static string Nodetext(string path,string node) {
CreateXMLElement( path); //创建XML文档对象的方法,上面第3题
return xmlDocument.SelectSingleNode(node).InnerText;
}
public static string Nodetexts(string path, string node,string nodes) {
CreateXMLElement(path);
return xmlDocument.SelectSingleNode(node).Attributes[nodes].Value;
}
6:文件写入流,文件读取流是哪个?
public static class Reader
{
public static BookStore store = new BookStore();
public static void Read() {
store.books = new List<Book>();
XmlDocument doc = new XmlDocument();
doc.Load("@D:\\str.xml");
XmlElement xmlElement = doc.DocumentElement;
foreach (XmlNode item in xmlElement.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);
}
}
public static void Bian() {
foreach (var item in store.books)
{
Console.WriteLine("Type:"+ item.Type+ "ISBN"+item.ISBN+ "title"+item.title+ "author:"+item.author+ "price"+item.price);
}
}
}
7:文件写入流,文件读取流是哪个?
string path = null;
FileStream fileStream = new FileStream( path, FileMode.Create);
8【代码】实现读取指定目录的文件内容
public static class Real
{
public static void Reals(string path) {
FileStream fileStream = new FileStream(path,FileMode.Open );
StreamReader streamReader = new StreamReader(fileStream);
string cont= streamReader.ReadToEnd();
streamWriter .close();
fileStream .close();
}
}
9:复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
public static bool FilesExist(string path)
{
if (FilesExist(path)) {
return true;
}
return false;
}
public static void FilesMove(string path,string newpath)
{
if (FilesExist(path)) {
File.Move(path,newpath);
}
}
public static void FilesCopy(string path, string newpath)
{
if (FilesExist(path))
{
File.Copy(path, newpath);
}
}
public static void FilesDelete(string path)
{
if (FilesExist(path))
{
File.Delete(path);
}
}