• XML解析与文件存取


    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    namespace XMLDemo
    {
       class Program
        {
           static void Main(string[] args)
            {
               var xmlDoc = new XmlDocument();
                xmlDoc.Load("ClassLibrary1.XML");
               XmlElement rootElement = xmlDoc.DocumentElement;
               string sPath = "test.txt";
               FileStream fs = new FileStream(sPath, FileMode.Create);
               StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
               if (rootElement != null)
                {
                   XmlNodeList memberNode = rootElement.GetElementsByTagName("members");
                   if (memberNode.Count != 0)
                    {
                       XmlNode xn = memberNode.Item(0);
                       XmlNodeList xnls = xn.ChildNodes;
                       foreach (XmlNode xnl in xnls)
                       {
                           XmlElement xe = (XmlElement)xnl;
                          var className = xe.GetAttribute("name");
                           sw.WriteLine("method:{0}", className);
                           foreach (XmlNode childNode in xnl.ChildNodes)
                            {
                               var name = childNode.Name;  
                                var text = childNode.InnerText.Trim();
                               var contentName = name + ": " + text;
                                sw.WriteLine(contentName);
                            }
                        }
                    }
                }
                sw.Flush();
                sw.Close();
                fs.Close();
               Console.ReadKey();
            }
        }
    }
    namespace TestData
    {
        class Program
        {
            static void Main(string[] args)
            {
                //文件地址
                string txtFilePath = @"D:	estData.txt";
                //定义char 数组
                //利用FileStream类将文件文本数据变成流然后放入StreamReader 构造函数中
                using (FileStream stream = File.OpenRead(txtFilePath))
                {
                    using (StreamReader reader = new StreamReader(stream,Encoding.Default, false))
                    {
                        int i = 0;
                        string result = string.Empty;
                        while ((result = reader.ReadLine()) != null) 
                        {
                            Console.WriteLine("第{0}行的内容{1}", i, result);
                             i++;
                        }
                    }
                }
                Console.ReadKey();
            }
        }
    }
    

      

  • 相关阅读:
    数据库复习笔记
    mysql基础实验过程+遇到的问题的解决方法(error105处理)
    R文件变红原因to按钮变色的优化
    windos命令行设置网络
    牛客网-21天刷题计划-第2节 进阶-对称的二叉树
    0型文法、1型文法、2型文法、3型文法对照
    ES练习遇到错误
    安装kafka
    使用ES时踩过的坑
    前端报错
  • 原文地址:https://www.cnblogs.com/gylhaut/p/5665010.html
Copyright © 2020-2023  润新知