locked
Read xml & create directory C# RRS feed

  • Question

  • User1954082887 posted
    <City title="CityOne">
      <Companies title="Comp1">
        <Company>A</Company>
        <Company>P</Company>
        <Company>R</Company>
      </Companies>
      <Companies title="Comp2">
        <Company>D</Company>
        <Company>F</Company>
        <Company>H</Company>
      </Companies>
    </City>

    Like first create folder CityOne then under that two folders Comp1 & Comp2. Then under them respective folders A,P,R (under Comp1) D,F,H (under Comp2).

    Please let me know how to do the same in C#

    Tuesday, June 4, 2013 6:54 AM

Answers

  • User1557244751 posted

    Try below code

    var doc = XDocument.Parse("XML string").Descendants("City")
                    .Select(x => new
                    {
                        Title = x.Attribute("title").Value,
                        Companies = new
                        {
                            Title = x.Descendants("Companies").Select(y => new
                            {
                                Title = y.Attribute("title").Value,
                                Name = y.Descendants("Company").Select(z => z.Value).ToList()
                            }).ToList()
                        }
                    }).ToList();
                

    Above code is to read XML string and put it in a variable

    Now below use below code to create directories

    var basePath = @"c:/temp/";
    
                foreach (var item in doc)
                {
                    var baseDirPath = Path.Combine(basePath + item.Title);
                    Directory.CreateDirectory(baseDirPath);
                    foreach (var itm in item.Companies.Title)
                    {
                        var innerPath = Path.Combine(item.Title, itm.Title);
                        Directory.CreateDirectory(Path.Combine(basePath, innerPath));
                        foreach (var i in itm.Name)
                        {
                            Directory.CreateDirectory(Path.Combine(basePath, innerPath, i));
                        }
                    }
                }

    variable basePath is used to point base directory from where folder will be created.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2013 8:05 AM

All replies

  • User1557244751 posted

    Try below code

    var doc = XDocument.Parse("XML string").Descendants("City")
                    .Select(x => new
                    {
                        Title = x.Attribute("title").Value,
                        Companies = new
                        {
                            Title = x.Descendants("Companies").Select(y => new
                            {
                                Title = y.Attribute("title").Value,
                                Name = y.Descendants("Company").Select(z => z.Value).ToList()
                            }).ToList()
                        }
                    }).ToList();
                

    Above code is to read XML string and put it in a variable

    Now below use below code to create directories

    var basePath = @"c:/temp/";
    
                foreach (var item in doc)
                {
                    var baseDirPath = Path.Combine(basePath + item.Title);
                    Directory.CreateDirectory(baseDirPath);
                    foreach (var itm in item.Companies.Title)
                    {
                        var innerPath = Path.Combine(item.Title, itm.Title);
                        Directory.CreateDirectory(Path.Combine(basePath, innerPath));
                        foreach (var i in itm.Name)
                        {
                            Directory.CreateDirectory(Path.Combine(basePath, innerPath, i));
                        }
                    }
                }

    variable basePath is used to point base directory from where folder will be created.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2013 8:05 AM
  • User1954082887 posted

    very efficient approach Thanks Vijay

    Wednesday, June 5, 2013 2:23 AM