0

I have a problem with writing a CSV file to XML.

CSV File

[Folder];;;;;
[Folder\Subfolder1];;;;;
[Folder\Subfolder1\Subfolder2];;;;;
"Descriptive stuff";"its a password";"user or something";"a URL";"if anyone has a comment"
[Folder\Subfolder1\Subfolder3];;;;;
"Descriptive stuff";"its a password";"user or something";"a URL";"if anyone has a comment"

XML Output

    <?xml version="1.0" encoding="utf-8"?>
<PASSWORDFILE xmlns="http://www.password-depot.de/schemas/passwordfile/6.0/passwordfile.xsd">
    <PASSWORDS>
        <GROUP NAME="Folder">
            <GROUP NAME="Subfolder1">
                <GROUP NAME="Subfolder2">
                    <ITEM>
                        <DESCRIPTION>Descriptive stuff</DESCRIPTION>
                        <PASSWORD>its a password</PASSWORD>
                        <USERNAME>user or something</USERNAME>
                        <URL>a URL</URL>
                        <COMMENT>if anyone has a comment</COMMENT>
                    </ITEM>
                </GROUP>
                <GROUP NAME="Subfolder3">
                    <ITEM>
                        <DESCRIPTION>Descriptive stuff</DESCRIPTION>
                        <PASSWORD>its a password</PASSWORD>
                        <USERNAME>user or something</USERNAME>
                        <URL>a URL</URL>
                        <COMMENT>if anyone has a comment</COMMENT>
                    </ITEM>
                </GROUP>
            </GROUP>
        </GROUP>
    </PASSWORDS>
</PASSWORDFILE>

My problem is solely, that I don't know how to close the GROUP Tag after I have used it. At the moment I just close the GROUP Tag at the very end of the XML making a huge massive tree.

4
  • now it is. Btw. CSV import works very well in powershell so I'm going to guess it is a CSV Commented May 9, 2012 at 9:41
  • Hm, I wouldn't actually treat it as CSV in either case. CSV by definition is just a flat list of records, each with the same columns (properties). What you have there is actually a description of a hierarchy, so you need some sort of a parser. It's probably possible in half a screen of code or so but I can't really think about it right now. Probably maintain a stack of Folders and its current subfolders and check where a new group needs to belong, closing GROUP elements as you pop from the stack. It's definitely not a one-liner, though. Commented May 9, 2012 at 10:23
  • that much is apparent, I'm on around 100 lines of bad code right now. But I am stuck on how to make the hierarchy. And there is a reason for me changing from CSV to XML since it's as you say not stacked Commented May 9, 2012 at 10:48
  • Hm, it shouldn't need 100 lines. I'd think more around 30 or so. I'll try thinking of something later. Work calls right now, though. Commented May 9, 2012 at 10:55

1 Answer 1

1

First of all your text is not CSV however if you are fixed with this format you sure can convert to XML by writing custom DLL. You just need to understand the CSV file details and then use XElement/XAttribute to create an XML.

I just use the following code to convert into a DLL and then save as XML(Be noted that the XML creation code is bind the the CSV file which is provided in the link below:

http://msdn.microsoft.com/en-us/library/bb387090.aspx

Here is my custom DLL Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace CSVtoXML
{
public class Actions
 {
    public Actions()
    {

    }

    public static void convertCSVtoXML(string csvPath)
    {

        string[] source = File.ReadAllLines(csvPath);
        XElement cust = new XElement("Root",
            from str in source
            let fields = str.Split(',')
            select new XElement("Customer",
                new XAttribute("CustomerID", fields[0]),
                new XElement("CompanyName", fields[1]),
                new XElement("ContactName", fields[2]),
                new XElement("ContactTitle", fields[3]),
                new XElement("Phone", fields[4]),
                new XElement("FullAddress",
                    new XElement("Address", fields[5]),
                    new XElement("City", fields[6]),
                    new XElement("Region", fields[7]),
                    new XElement("PostalCode", fields[8]),
                    new XElement("Country", fields[9])
                )
            )
        ); 
        cust.Save("result.xml");
    }
 }
}

Here is how I used this DLL in powershell:

  • PS> [Reflection.Assembly]::LoadFile(“CSVtoXML.dll”)
  • PS> [CSVtoXML.Actions]::convertCSVtoXML("data.csv")

This will generate the "Result.xml"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.