C#XML文件

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>

       

 XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration declaration = xmldoc .CreateXmlDeclaration("1.0", "utf-8", null);
            xmldoc .AppendChild(declaration);
            XmlElement bookstore = xmldoc .CreateElement("bookstore");
            xmldoc .AppendChild(bookstore);
            XmlElement book = xmldoc.CreateElement("book");
            book.SetAttribute("Type", "必修课");
            book.SetAttribute("ISBN", "7-111-19149-2");
            bookstore.AppendChild(book);
            XmlElement title = xmldoc.CreateElement("title");
            title.InnerText = "数据结构";
            book.AppendChild(title);
            XmlElement author = xmldoc.CreateElement("author");
            author.InnerText = "严蔚敏";
            book.AppendChild(author);
            XmlElement price = xmldoc.CreateElement("price");
            price.InnerText = "30.00";
            book.AppendChild(price);
            xmldoc .Save(@"D:\\str.xml");

3:创建XML文档对象的类,创建XML头的类,创建XML节点的类分别是哪个?
//XML文档对象    XmlDocument 
//创建XML头的类   XmlDeclaration
//创建XML节点 XmlElement
4:节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是?
节点添加?AppendChild()


保存XML Save()


加载XML Load()


读取XML节点??
  XmlDocument xmldoc = new XmlDocument();
  XmlElement rootElem = xmldoc .DocumentElement;   //获取根节点    
  XmlNodeList personNodes = rootElem.GetElementsByTagName("xxx"); //获取xxx子节点集合    
foreach (XmlNode node in personNodes){}
5:【代码】读取节点的值,读取节点属性的值?
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<Book> books;
Book类有以下属性:
Type,ISBN,title,author,price

             XmlNode node = doc.SelectSingleNode("xxx");
            foreach (XmlNode item in node.ChildNodes) 
            {
                book.Type = item.Attributes["Type"].Value;
                book.ISBN = item.Attributes["ISBN"].Value;
                book.title = item["title"].InnerText;    
                book.author = item["author"].InnerText; 
            }


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

文件读取流  StreamReader
7:【代码】实现读取指定目录的文件内容
 FileStream fs = new FileStream(path, FileMode.Create);
         

   StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.Write("Hello");
            sw.Close();
            fs.Close();


8:【代码】实现写入指定目录的文件内容

FileStream fs = new FileStream(path, FileMode.Create);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            sr.ReadToEnd();
            sr.Close();
            fs.Close();


9:复制文件,移动文件,删除文件,判断文件是否存在,读取指定目录下的所有目录的方法分别是?
复制文件 File.Copy(原位置复制到的位置 );

移动文件  File.Move();

删除文件 File.Delete();

判断文件是否存在 File.Exists();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值