简体   繁体   中英

Create an XML file from a text file

I have a text file like that contains contact information like this:

martin@actionbase.se, Martin Grape, HTML
pedram@actionbase.se, Pedram Mebedi, HTML

I need to convert the above information into proper XML form like:

<?xml version="1.0" encoding="UTF-8"?>
<Subscribers>
    <Subscriber>
        <Name>Martin Grape</Name>
        <Email>martin@actionbase.se</Email>
        <Format>HTML</Format>
    </Subscriber>
    <Subscriber>
        <Name>Pedram Mebedi</Name>
        <Email>pedram@actionbase.se</Email>
        <Format>HTML</Format>
    </Subscriber>
</Subscribers>

And I need to do this pragmatically. So do I have to come up with my own algorithm or is there a faster way to this? Thanks.

It depends on which platform you are using and I can't speak for all but I think you'll need to come up with your own code to convert this into XML. Should be pretty simple though as you can just read each line and split it on the comma to get each item of data.

YOu can use different tools like SoftSilver .

YOu can check out this link. It will surely help you:- http://www.devx.com/getHelpOn/10MinuteSolution/20356

This is inspiration in C#, you can easily convert to VB.NET and extend:

var xml = new StringBuilder();

xml.Append("<Subscribers>");

foreach(var line in System.IO.File.ReadAllLines(@"yourfile.txt"))
{
    var vals = line.Split(',');

    // TODO add more fields
    xml.AppendFormat("<Subscriber><Name>{0}</Name><Email>{1}</Email></Subscriber>",
        vals[0].Trim(), vals[1].Trim());

}

xml.Append("</Subscribers>");

System.IO.File.WriteAllText(@"your.xml", xml.ToString());

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