简体   繁体   中英

Convert flat text file to XML in C#

I have a .txt file and I have to change it in xml file. My .txt file is pipe-delimited ("|", vertical bar) flat text file. Like this:

169055|759656025621|Dos|Justamente Tres|Kill Rock Stars|256|PUNK|CD-JEWEL CASE|06/24/1996|D

Now I have to change this text file into xml file and also I have to add parent-child node for this xml. I have to use Linq to xml and XElement. Please help me out.

//path of your RDF file i.e. txt file used delimeter |

String filePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"RdfFile/RDF.txt");

IEnumerable<String> source = File.ReadLines(filePath);

 XElement scans = new XElement("Test",


        from str in source
        let fields = str.Split('|')
        select new XElement("Product",
           new XAttribute("Action", FileName),
           new XAttribute("EnsureDefaultVariant", "1"),
           new XAttribute("ID", fields[0]),



            new XElement("Summary", fields[2]),
            new XElement("ImageFilenameOverride", fields[1])


            )

    );
//path in which xml file you want to save result.

 String filePath_Xml = Path.Combine(HostingEnvironment.ApplicationPhysicalPath,
 @"XMLFile/RDF__Scans_Add_XMLFile.xml");

 scans.Save(filePath_Xml);

Place your input text file as c:\\sample.txt . Containing lines like (number of separators could vary, reulting more or less fields for every record) :

169055|759656025621|Dos|Justamente Tres|Kill Rock Stars|256|PUNK|CD-JEWEL CASE|06/24/1996|D
xx|xx|xx|xx|xx|xx|xx|xx|xx|xx
...

And then you may start like this:

public static void Main() {
            XElement root = new XElement("root");//create a root node
            foreach (String ln in File.ReadAllLines(@"c:\sample.txt")){//<-- read lines
                string[] fields = ln.Split('|'); //<-- change field separator here & split fields
                XElement record = new XElement("record"); //create a child node (i.e., parent of filelds)
                int pos = 0;
                foreach (String sp in fields){
                    pos += 1;
                    XElement field = new XElement(string.Format("field_{0}", pos.ToString())); // prepare child nodes
                    field.Add(sp);
                    record.Add(field); // add to parent node
                }
                root.Add(record); // add to root
            }
            Console.Write (root.ToString());  // display the result on console
            Console.ReadKey(); // waiting for you........
        }

Result on console:

<root>
  <record>
    <field_1>169055</field_1>
    <field_2>759656025621</field_2>
    <field_3>Dos</field_3>
    <field_4>Justamente Tres</field_4>
    <field_5>Kill Rock Stars</field_5>
    <field_6>256</field_6>
    <field_7>PUNK</field_7>
    <field_8>CD-JEWEL CASE</field_8>
    <field_9>06/24/1996</field_9>
    <field_10>D</field_10>
  </record>
  <record>
    <field_1>xx</field_1>
    <field_2>xx</field_2>
    <field_3>xx</field_3>
    <field_4>xx</field_4>
    <field_5>xx</field_5>
    <field_6>xx</field_6>
    <field_7>xx</field_7>
    <field_8>xx</field_8>
    <field_9>xx</field_9>
    <field_10>xx</field_10>
  </record>
</root>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM