computing
  • 0

Solved Converting .Txt File To .Xml File Using C#

  • 0

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

Share

1 Answer

  1. 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

    • 0