化工复试自用2:txt与xml读写相关

1.读写txt的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace 读写txt
{
    internal class Program
    {
        private const string FILE_NAME = "Test.txt";
        static void Main(string[] args)
        {
            WriteValues();
            DisplayValues();
        }

        public static void DisplayValues()
        {
            if (!File.Exists(FILE_NAME))
            {
                Console.WriteLine("{0}不存在", FILE_NAME);
                Console.ReadLine();
                return;
            }
            FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);//新建文件流
            StreamReader r = new StreamReader(fs);//通过阅读工具读出
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(r.ReadLine());
            }

            r.Close();
            fs.Close();

            Console.ReadLine();
        }

        public static void WriteValues()
        {
            if (File.Exists(FILE_NAME))
            {
                Console.WriteLine("{0}已存在", FILE_NAME);
                Console.ReadLine();
                return ;
            }
            FileStream fs=new FileStream(FILE_NAME, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter w = new StreamWriter(fs);
            for(int i = 0; i < 4; i++)
            {
                w.WriteLine("Access");
            }

            w.Close();
            fs.Close();
        }
    }

2.读写xml的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace 读写xml
{
    public class RWXml
    {
        //生成一个xml文档
        public void WriteXml()
        {
            //random
            Random rd = new Random();
            //创建xml文档
            XmlDocument xDoc = new XmlDocument();
            XmlDeclaration declaration=xDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            xDoc.AppendChild(declaration);//添加xml声明
            XmlElement elem=xDoc.CreateElement("students");//创建根节点students
            xDoc.AppendChild(elem);
            for (int i = 1; i <= 10; i++)
            {
                XmlElement elem1 = xDoc.CreateElement("student");
                elem.AppendChild(elem1);//添加子节点
                elem1.SetAttribute("姓名", "张三");//添加属性
                elem1.SetAttribute("学号", i.ToString());
                XmlElement elem1_1 = xDoc.CreateElement("语文成绩");
                elem1.AppendChild(elem1_1);
                elem1_1.InnerText = rd.Next(50,90).ToString();
                XmlElement elem1_2 = xDoc.CreateElement("数学成绩");
                elem1.AppendChild(elem1_2);
                elem1_2.InnerText = rd.Next(30,100).ToString();
                XmlElement elem1_3 = xDoc.CreateElement("英语成绩");
                elem1.AppendChild(elem1_3);
                elem1_3.InnerText = rd.Next(50,95).ToString();
            }



            xDoc.Save("student.xml");
        }
        //读取
        public void ReadXml()
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("student.xml");
            XmlNode node=xDoc.SelectSingleNode("students");
            XmlNodeList nodeList=node.ChildNodes;//取子节点集合
            foreach(XmlNode xn in nodeList)
            {
                //类型显示转换,取属性
                XmlElement xmle=(XmlElement)xn;
                string name = xmle.GetAttribute("姓名");
                string no = xmle.GetAttribute("学号");
                Console.WriteLine("姓名:"+name+"\n"+"学号:"+no);
                XmlNodeList ChildList = xn.ChildNodes;//取子节点的子节点集合
                foreach(XmlNode n in ChildList)
                {
                    XmlElement xe = (XmlElement)n;
                    string classname = xe.Name;//标签名
                    string score=xe.InnerText;//内部文本
                    Console.WriteLine(classname+":"+score);

                    if (no == "10" && classname == "数学成绩")
                    {
                        xe.InnerText="100";//修改内部文本
                    }
                    

                }
            }
            xDoc.Save("student.xml");//若有修改记得保存
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 读写xml
{
    internal class Program
    {
        static void Main(string[] args)
        {
            RWXml xml = new RWXml();
            //xml.WriteXml();
            xml.ReadXml();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值