Click here to Skip to main content
14,738,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have Excel with Path information like:
\nagmonbashare05\IBCMA_UAT\1. Completed Deal File\1. Confidentiality agreements\
\nagmonbashare05\IBCMA_UAT\1. Completed Deal File\2. Engagement letter\
\nagmonbashare05\IBCAdmin\270Park\client information\Cars\dat\

Basically, it can be nth level folder nesting within the same parent say 'nagmonbashare05'.

How i can generate XML file with each folders and elements? The expected output should be something like:
<?xml version="1.0" encoding="utf-8" ?>
    <Folder id='\SNJCARFPT4NA05_IB_NAS1' name='SNJCARFPT4NA05_IB_NAS1'>
        <Folder id='\nagmonbashare05' name='nagmonbashare05'>
            <Folder id='\nagmonbashare05\IBCAdmin' name='IBCAdmin'>
                <Folder id='\nagmonbashare05\IBCAdmin\270Park' name='270Park'>
                    <Folder id='\nagmonbashare05\IBCAdmin\270Park\Markets' name='Markets'>
Updated 30-Dec-10 17:37pm

1 solution

I needed a quick mental challenge and was feeling particularly generous tonight.

List<string> file = new List<string>()
    @"\nagmonbashare05\IBCMA_UAT\1. Completed Deal File\1. Confidentiality agreements\",
    @"\nagmonbashare05\IBCMA_UAT\1. Completed Deal File\2. Engagement letter\",
    @"\nagmonbashare05\IBCAdmin\270Park\client information\Cars\dat\"

XDocument doc = new XDocument();

foreach(string line in file)
    XElement parentElement = null;

    foreach(string folderName in line.Split(new char[]{'\\'}, StringSplitOptions.RemoveEmptyEntries) )
        XElement folder = (from e in doc.Descendants("Folder")
                            where e.Attribute("name").Value == folderName
                            select e)

        if(folder == null && parentElement == null)
            parentElement = new XElement("Folder",
                new XAttribute("name", folderName));

        else if(folder == null)
            XElement folderElement = new XElement("Folder",
                new XAttribute("name", folderName));


            parentElement = folderElement;
            parentElement = folder;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900