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(); } } }