将文本文件数据转换为Xml文件的代码 [英] Code to converting Text File Data into Xml File

查看:75
本文介绍了将文本文件数据转换为Xml文件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我的要求是
首先,它应该从文本文件中读取数据,然后将数据写入xml文件中.

here my requirement is
first it should read data from the text file and then write data into xml file

推荐答案

确实,这取决于您在您的文件中,以及您要如何处理.但是...
Really, that is going to depend on wht you have in your file, and what you want to do with it. But...
string text = File.ReadAllText(@"D:\Temp\myFile.txt");
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("MyRoot");
doc.AppendChild(root);
XmlElement elem = doc.CreateElement("MyElement");
elem.SetAttribute("MyData", text);
root.AppendChild(elem);
File.WriteAllText(@"D:\Temp\myFile.xml", doc.OuterXml);


这篇关于将文本文件数据转换为Xml文件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆