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();
}
}
}
XAML导出到TXT
最新推荐文章于 2024-01-11 21:40:40 发布