C#笔记——内含XML、文件操作的基本语法等

1. 什么是XML?

  • XML 指可扩展标记语言(EXtensible Markup Language)
  • XML 的设计宗旨是传输数据,而非显示数据
  • XML 被设计为具有自我描述性。
  • XML 是 W3C 的推荐标准
  • 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 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);
}

3. 创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个

创建XML文档对象的类 —— XmlDocument( )
创建XML头的类 —— XmlDeclaration.CreateXmlDeclaration( )
创建XML节点的类 —— XmlElement.CreateElement( )

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

节点添加方法 —— XmlElement.CreateElement( )
保存XML方法 —— Xml.Save( )
加载XML方法 —— Xml.Load( )
读取XML节点 —— XmlDocument.DocumentElement( )

5. 读取节点的值以及读取节点属性的值

读取节点的值

           //读取xml节点下的值
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.LoadXml(result);
           XmlNode root = xmlDoc.SelectSingleNode("//response");
           if (root != null)
            {
                string error = (root.SelectSingleNode("error")).InnerText;
            }

读取节点属性的值

//读取节点属性的值
public List<string> GetAttribute(string xmlFile, string nodeName, string attributeName)
 {
 List<string> retList = new List<string>();
 XmlDocument xx = new XmlDocument();
 xx.Load(xmlFile);
 XmlNodeList xxList = xx.GetElementsByTagName(nodeName);
 foreach (XmlNode xxNode in xxList)
 {
 retList.Add(xxNode.Attributes[attributeName].Value);
 }
 return retList;
 } 

6. 将以下格式

<?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<Book> books;
Book类有以下属性:
Type,ISBN,title,author,price

以下是解答

BookStore store = new BookStore();
store.books = new List<Book>();
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\\str.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);
}

7. 实现读取指定目录的文件内容

public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
            {
                //如果目录不存在,则抛出异常
                if (!IsExistDirectory(directoryPath))
                {
                    throw new FileNotFoundException();
                }
                try
                {
                    if (isSearchChild)
                    {
                        return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
                    }
                    else
                    {
                        return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
                    }
                }
                catch (IOException ex)
                {
                    throw ex;
                }
            }

8. 实现写入指定目录的文件内容

public static void WriteText(string filePath, string text, Encoding encoding)
  {
   //向文件写入内容
   File.WriteAllText(filePath, text, encoding);
  }

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

文件写入流 —— StreamWriter.Write()
文件读取流 —— StreamReader.ReadLine()

10. 复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是

复制文件 —— File.Copy()
移动文件 —— File.Move()
删除文件 —— File.Delete()
判断文件是否存在 —— File.Exists()
读取指定目录下的所有目录 —— DirectoryInfo.GetFiles()
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值