Solved converting .txt file to .xml file using C#

October 15, 2014 at 05:35:29
Specs: windows
I have a .txt file and i want to convert it into .xml file using c#. The txt file looks like

SCRIPT=01

Mainversion=1.00.00.00:01

This is my C# code

String[] data = File.ReadAllLines("TextFile.txt");
XElement root = new XElement("root",
from item in data
select new XElement("Line",item));
root.Save("XmlFile.Xml");
while executing this i am getting an output as

<line>script=01</line>

<line>Mainversion=1.00.00.00:01</line>

but my expected output is

<script>01</script>

<mainversion>1.00.00.00:01</mainversion>

thanks in advance


See More: converting .txt file to .xml file using C#

Report •


#1
October 17, 2014 at 03:59:10
So split your line, and base your XElement on that split?

How To Ask Questions The Smart Way


Report •

#2
October 25, 2014 at 18:02:00
✔ Best Answer
As Razor2.3 stated...


Instead of:

select new XElement("Line",item));
root.Save("XmlFile.Xml");

do:

string[] myItem = item.split('=');
select new XElement(myItem[0],myItem[1]));
root.Save("XmlFile.Xml");

Obviously you'll get a whole lot of screwed up stuff if a particular line in your text file has more than one "=" in it but if it doesn't then you're home free!

message edited by FBI Agent


Report •
Related Solutions


Ask Question