化工复试自用3:txt与xml互相转化

输入txt,输出xml

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

namespace 输入txt输出xml
{
    internal class Program
    {
        static void Main(string[] args)
        {
            TxtToXml();
        }

        public static void TxtToXml()
        {
            //读文件
            //一行一行地读,然后将整个行字符串拆分为多个子串形成一个字符串数组
            //记得这里要用Encoding.GetEncoding("utf-8"),不然会出现乱码
            StreamReader sr = new StreamReader(File.Open(@"C:\Users\output.txt", FileMode.Open), Encoding.GetEncoding("utf-8"));

            //ArrayList mydata = new ArrayList();

            //输出到xml
            //创建xml文档
            //实例化一个xmldocument类
            XmlDocument xDoc = new XmlDocument();

            //声明一个xml所需要的语法的变量,添加在末尾
            XmlDeclaration xmlDeclaration = xDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");

            //创建根节点
            XmlElement element = xDoc.CreateElement("grades");

            //添加根节点到xml文档去
            xDoc.AppendChild(element);

            while (true)
            {
                //读取每一行的数据
                string line = sr.ReadLine();
                if (line == string.Empty || line == null) break;
                char[] separator = { ' ' };
                //split:按照separate拆分成子串,返回给data字符串数组
                string[] data = line.Split(separator);

                //构造结点grade
                XmlElement element1 = xDoc.CreateElement("grade");
                //插入到根节点下
                element.AppendChild(element1);

                //学号
                //创建grade下面的子节点Id
                XmlElement element1_1 = xDoc.CreateElement("Id");
                element1.AppendChild(element1_1);
                element1_1.InnerText = data[0];

                //姓名
                XmlElement element1_2 = xDoc.CreateElement("Name");
                element1.AppendChild(element1_2);
                element1_2.InnerText = data[1];

                //课程
                XmlElement element1_3 = xDoc.CreateElement("Course");
                element1.AppendChild(element1_3);
                element1_3.InnerText = data[2];

                //分数
                XmlElement element1_4 = xDoc.CreateElement("Score");
                element1.AppendChild(element1_4);
                element1_4.InnerText = data[3];
                //输出到grades.xml
                xDoc.Save("grades.xml");
            }
        }
    }
}

输入xml,输出txt

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

namespace 输入xml_输出txt
{
    internal class Program
    {
        static void Main(string[] args)
        {
            XmlToTxt();
        }

        public static void XmlToTxt()
        {
            string[] str = new String[20];
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("C:/Users/grades.xml");
            XmlNode node = xDoc.SelectSingleNode("grades");
            XmlNodeList nodeList = node.ChildNodes;//取子节点集合
            int i = 0;
            foreach (XmlNode xn in nodeList)
            {              
                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);
                    str[i]=classname + ":" + score;
                    i++;
                }
            }
            string outputPath = "C:/Users/input.txt";
            File.WriteAllLines(outputPath, str);
            Console.ReadKey();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值