zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    

      

  • 相关阅读:
    RAID中条带的概念
    关于几个与IO相关的重要概念
    分布式调度
    ajax
    choices参数
    1.Python实现字符串反转的几种方法
    django web框架
    CRM总结
    Python面试重点(web篇)
    day02-网编并发数据库
  • 原文地址:https://www.cnblogs.com/gylhaut/p/5665010.html
Copyright © 2011-2022 走看看