XAML导出到TXT

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

namespace ReadAndWriteXML
{
    class Program
    {
        static void Main(string[] args)
        {
            //循环执行
            while(true)
            {
                if (Info() == true)
                {
                    Console.WriteLine("执行结果:Success!");
                }
                else
                {
                    Console.WriteLine("执行结果:Fail!");
                }
            }
        }
        /// <summary>
        /// 获取文件路径和设置文件名称
        /// </summary>
        /// <returns></returns>
        public static bool Info()
        {
            string fileName;
            string configPath;
            Console.WriteLine("文件路径:");
            //读取文件路径
            configPath = Console.ReadLine();
            Console.WriteLine("文件名称:");
            //读取文件名称
            fileName = Console.ReadLine();
            try
            {
                ReadXml(fileName, configPath);
            }
            catch (Exception e)
            {
                //显示异常信息
                Console.WriteLine("Log:" + e.Message);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 读取xmal文档
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="configPath"></param>
        public static void ReadXml(string fileName,string configPath)
        {
            //把字符串转化为文件路径
            configPath = string.Format(@"{0}", configPath);
            XmlDocument xmlDoc = new XmlDocument();
            //忽略xmal的注释信息
            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.IgnoreComments = true;
            XmlReader xmlReader = XmlReader.Create(configPath, xmlReaderSettings);
            //加载xmal文件
            xmlDoc.Load(xmlReader);
            XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
            xmlNamespaceManager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            xmlNamespaceManager.AddNamespace("sys", "clr-namespace:System;assembly=mscorlib");
            XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
            int i = 0;
            foreach (XmlNode item in xmlNodeList)
            {
                i++;             
                WritLog(fileName,item.InnerText);
                Console.WriteLine("Waiting{0}!", i);
                //Console.WriteLine("{0}\t{1}",i,item.InnerText);
            }

        }
        /// <summary>
        /// 把xmal文件内容写入txt
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        public static void WritLog(string fileName,string content)
        {
            //获取桌面路径
            string deskPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            //文件存储绝对路径
            fileName = string.Format(@"{0}\{1}.txt",deskPath,fileName);
            FileStream fs;
            StreamWriter sw;
            //判断文件是否存在
            if (File.Exists(fileName))
            {
                //存在该文件则追加,往文件写
                fs = new FileStream(fileName, FileMode.Append, FileAccess.Write);
            }
            else
            {
                //不存在则创建文件,往文件写
                fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            }
            sw = new StreamWriter(fs);
            sw.WriteLine(content);
            sw.Close();
            fs.Close();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值