XML读写方法基础要点

1:什么是XML?
xml是可扩展标记性语言

2:【代码】输入以下xml格式,并生成bookstore.xml文件

<?xml version="1.0" encoding="utf-8"?> 数据结构 严蔚敏 30.00

static void Main(string[] args)
{
XmlDocument xmlDocument = new XmlDocument();
XmlDeclaration declaration = xmlDocument.CreateXmlDeclaration(“1.0”, “utf-8”, null);
xmlDocument.AppendChild(declaration);
XmlElement store= xmlDocument.CreateElement(“bookstore”);
xmlDocument.AppendChild(store);
XmlElement stu = xmlDocument.CreateElement(“book”);
xmlDocument.AppendChild(stu);
stu.SetAttribute(“Type”, “必修课”);
stu.SetAttribute(“ISBN”, “7-111-19149-2”);
XmlElement title = xmlDocument.CreateElement(“title”);
title.InnerText = “数据结构”;
XmlElement author = xmlDocument.CreateElement(“author”);
author.InnerText = “严蔚敏”;
XmlElement price = xmlDocument.CreateElement(“price”);
price.InnerText = “30.00”;
stu.AppendChild(title);
stu.AppendChild(author);
stu.AppendChild(price);
xmlDocument.Save(“D:\a.xml”);
}
3:创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?

文档对象
XmlDocument xd = new XmlDocument();


XmlDeclaration;

节点的类
:XmlElement;

4.节点添加方法,保存XML方法,加载Xml方法,读取XML节点方法分别是哪些?

添加
AppendChild();

保存
Save();

加载
Load();

读取
XmlNode xmlNode = xml.DocumentElement;
foreach遍历

5:【代码】读取节点的值,读取节点属性的值?:将以下格式

<?xml version="1.0" encoding="utf-8"?> 数据结构 严蔚敏 30.00 算法 严蔚敏 10.00 转换成类 BookStore 有以下属性:List books; Book类有以下属性: Type,ISBN,title,author,price XmlDocument xml = new XmlDocument(); xml.Load(@"E://Text.txt"); XmlNode engineer = xml.DocumentElement; BookStore bs= new BookStore(); foreach (XmlNode item in engineer.ChildNodes) { Book stu = new Book(); stu.Type = item.Attributes["Type"].Value;//读取属性值 stu.ISBN = item.Attributes["ISBN"].Value; stu.price = item["price"].InnerText;//读取文本值 stu.title = item["title"].InnerText; stu.author = item["author"].InnerText; bs.Books.Add(stu); }

6.文件写入流,文件读取流是哪个?

//写入
FileStream sha = new FileStream(this.textBox1.Text.Trim(),FileMode.Create);
StreamWriter sw = new StreamWriter(sha);

//读取
FileStream sha = new FileStream(this.textBox1.Text.Trim(), FileMode.Open);
StreamReader sr = new StreamReader(sha);

7.[代码]实现读取指定目录的文件内容

//创建文件流(路径,模式)
FileStream sha = new FileStream(this.textBox1.Text.Trim(), FileMode.Open);
//2.创建读取器(文件流)
StreamReader sr = new StreamReader(sha);
//读取操作
this.textBox2.Text = sr.ReadToEnd();
//关闭流
sr.Close();
sha.Close();

8.[代码]实现写入指定目录的文件内容

//创建文件流(路径,模式)
FileStream sha = new FileStream(this.textBox1.Text.Trim(),FileMode.Create);
//2.创建写入器(文件流)
StreamWriter sw = new StreamWriter(sha);
//读取操作
sw.Write(textBox2.Text);
//关闭流
sw.Close();
sha.Close();

9.复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
复制
File.Copy(“原路径”, “新路径”);
移除
File.Move(“原路径”, “新路径”);
删除
File.Delete(“路径”);
判断
是否存在:File.Exists(“路径”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值