C#XML文件

1:什么是XML?

可扩展标记语言(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>

static void Main(string[] args)
        {
            string path = @"C:\\bookstore.xml";
            FileStream fileStream = new FileStream(path,FileMode.Create);

            XmlDocument xml = new XmlDocument();
            XmlDeclaration xmlDeclaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(xmlDeclaration);

            XmlElement xmlElement = xml.CreateElement("bookstore");
            xml.AppendChild(xmlElement);

            XmlElement xmlElement1 = xml.CreateElement("book");
            xmlElement1.SetAttribute("Type", "必修课");
            xmlElement1.SetAttribute("ISBN", "7-111-19149-2");
            xmlElement.AppendChild(xmlElement1);

            XmlElement xmlElement2 = xml.CreateElement("title");
            xmlElement2.InnerText = "数据结构";
            xmlElement1.AppendChild(xmlElement2);

            XmlElement xmlElement3 = xml.CreateElement("author");
            xmlElement3.InnerText = "严蔚敏";
            xmlElement1.AppendChild(xmlElement3);

            XmlElement xmlElement4 = xml.CreateElement("price");
            xmlElement4.InnerText = "30.00";
            xmlElement1.AppendChild(xmlElement4);

            fileStream.Close();
            xml.Save(path);
        }

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

XmlDocument xml = new XmlDocument();
XmlDeclaration xmlDeclaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement xmlElement = xml.CreateElement("bookstore");

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

XmlDocument xml = new XmlDocument();
xml.AppendChild();
xml.Save(path);
xml.Load(path);
xml.SelectNodes();


5:【代码】读取节点的值,读取节点属性的值?

XmlDocument xml = new XmlDocument();
XmlElement xmlElement = xml.CreateElement("bookstore");
xmlElement.InnerText;
xmlElement.Attributes["id"].Value;


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

文件写入流:

string path = @"C:\\text.txt";
FileStream fileStream = new FileStream(path, FileMode.Create);

文件读取流:

string path = @"C:\\text.txt";
FileStream fileStream = new FileStream(path, FileMode.Create);
StreamWriter streamWriter = new StreamWriter(fileStream);

7:【代码】实现读取指定目录的文件内容

static void Main(string[] args)
        {
            FileStream file = null;
            StreamReader streamReader = null;
            try
            {
                string path = @"C:\\text.txt";
                file = new FileStream(path, FileMode.Open);
                streamReader = new StreamReader(file, Encoding.Default);
                string str = streamReader.ReadLine();
                Console.WriteLine(str);
                Console.ReadKey();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                file.Close();
                streamReader.Close();
            }
        }

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

static void Main(string[] args)
        {
            FileStream fileStream = null;
            StreamWriter streamWriter = null;
            try
            {
                string path = @"C:\\text.txt";
                fileStream = new FileStream(path, FileMode.Create);
                streamWriter = new StreamWriter(fileStream);
                string str = "HelloWorld";
                streamWriter.Write(str);
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                streamWriter.Close();
                fileStream.Close();
            }
        }

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

 public void Get()
        {
            string path = @"D:\\004继承多态\\005XML文件.txt";
            File.Copy(path, @"C:\\005XML文件.txt");
        }
public void Get()
        {
            string path = @"C:\\002铂金题.txt";
            File.Delete(path);
        }
public void Get()
        {
            string path = @"D:\\005XML文件\\002铂金题.txt";
            File.Move(path,"C:\\wj");
        }
 public void Get()
        {
            string path = @"D:\\004继承多态\\002铂金题.txt";
            if (File.Exists(path))
            {
                Console.WriteLine("存在");
            }
            else
            {
                Console.WriteLine("不存在");
            }
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值