Avatar of YourSystemExpert
YourSystemExpert
Flag for United States of America asked on

read XML file, create/modify folders, add files to each folder

Need some help modifing my code to work correctly. I'm Stumped.

What I would like to do is read a basic xml filed called web.sitemap. While walking through the xml file, create a directory for each node and childnode. The "title" attribute will be the name of the directory to be created. The "url" attribute is the location of the final directory that needs to be created. Once the directory has been created, copy 4 files to that folder. Then repeat process until the xml file has been completed.

I've attached some code that is somewhat working. The problem with the code is that it creates all the directories within the same folder. Then copies the 4 files to each folder. If anyone has a better way, please suggest.....

A small example of what my xml file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"  >
    <siteMapNode title="Home" url="~/default.aspx" description="Your System Expert Home Page">

        <siteMapNode title="Products" url="~/products/default.aspx" description="Products">
            <siteMapNode title="Computers" url="~/products/Computers/default.aspx" description="" >
                <siteMapNode title="Barebone" url="~/products/Computers/Barebone/default.aspx" description="" >
                    <siteMapNode title="Barebones PC" url="~/products/Computers/Barebone/Barebones_PC/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Barebones Servers" url="~/products/Computers/Barebone/Barebones_Servers/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Barebones Notebooks" url="~/products/Computers/Barebone/Barebones_Notebooks/DisplayProducts1.aspx" description="" />
                </siteMapNode>
                <siteMapNode title="Desktops" url="~/products/Computers/Desktops/default.aspx" description="" >
                    <siteMapNode title="Computer and Monitor Units" url="~/products/Computers/Desktops/Computer_and_Monitor_Units/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Desktop Systems" url="~/products/Computers/Desktops/Desktop_Systems/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Small Form- Ultra Slim Systems" url="~/products/Computers/Desktops/Small_Form-_Ultra_Slim_Systems/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Tower Systems" url="~/products/Computers/Desktops/Tower_Systems/DisplayProducts1.aspx" description="" />
                </siteMapNode>
                <siteMapNode title="Servers" url="~/products/Computers/Servers/default.aspx" description="" >
                    <siteMapNode title="Blade Servers" url="~/products/Computers/Servers/Blade_Servers/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Blade Server Components" url="~/products/Computers/Servers/Blade_Server_Components/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Pedestal Tower Servers" url="~/products/Computers/Servers/Pedestal_Tower_Servers/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Rackmount Servers" url="~/products/Computers/Servers/Rackmount_Servers/DisplayProducts1.aspx" description="" />
                </siteMapNode>
                <siteMapNode title="Thin Clients" url="~/products/Computers/Thin_Clients/default.aspx" description="" >
                    <siteMapNode title="All-In-One Thin Clients" url="~/products/Computers/Thin_Clients/All-In-One_Thin_Clients/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Thin Client Terminals" url="~/products/Computers/Thin_Clients/Thin_Client_Terminals/DisplayProducts1.aspx" description="" />
                </siteMapNode>
                <siteMapNode title="Terminals" url="~/products/Computers/Terminals/default.aspx" description="" >
                    <siteMapNode title="General Purpose Terminals" url="~/products/Computers/Terminals/General_Purpose_Terminals/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Terminal Compnents" url="~/products/Computers/Terminals/Terminal_Compnents/DisplayProducts1.aspx" description="" />
                </siteMapNode>
                <siteMapNode title="Workstations" url="~/products/Computers/Workstations/default.aspx" description="" >
                    <siteMapNode title="Itanium-Based Workstation" url="~/products/Computers/Workstations/Itanium-Based_Workstation/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="Opteron-Based Workstation" url="~/products/Computers/Workstations/Opteron-Based_Workstation/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="P4-Based Workstation" url="~/products/Computers/Workstations/P4-Based_Workstation/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="RISC-Based Workstation" url="~/products/Computers/Workstations/RISC-Based_Workstation/DisplayProducts1.aspx" description="" />
                    <siteMapNode title="XEON-Based Workstation" url="~/products/Computers/Workstations/XEON-Based_Workstation/DisplayProducts1.aspx" description="" />
                </siteMapNode>
       </siteMapNode>
</siteMapNode>



Dim xmlr As XmlTextReader
        Try
            xmlr = New XmlTextReader("d:\<removed for security reasons>\web.sitemap")
            xmlr.Read()
            While Not xmlr.EOF
                xmlr.Read()
 
                If Not xmlr.IsStartElement Then
                    Exit While
                End If
 
                Dim oTitle As String, rTitle As String
                oTitle = xmlr.GetAttribute("title")
 
                Dim dirName As String
                Dim dir As String = Request.PhysicalApplicationPath
 
                rTitle = Replace(oTitle, " ", "_")
                dirName = rTitle
 
                xmlr.Read()
                Dim dirstring As String
                dirstring = dir + "products\" & dirName
 
                If Directory.Exists(dirstring) Then
                    Response.Write(dirstring & " - ALREADY CREATED!<br>")
                Else
                    Directory.CreateDirectory(dirstring)
                    Response.Write(dirstring & " - Created!<br>")
                End If
 
                Dim fName1 As String = "Default.aspx"
                Dim fName2 As String = "DisplayProducts1.aspx"
                Dim fName3 As String = "Default.aspx.vb"
                Dim fName4 As String = "DisplayProducts1.aspx.vb"
 
                File.Copy("d:\<removed for security reasons>\cfolder\" & fName1, dir + "products\" & dirName & "\" & fName1)
                File.Copy("d:\<removed for security reasons>\cfolder\" & fName2, dir + "products\" & dirName & "\" & fName2)
                File.Copy("d:\<removed for security reasons>\cfolder\" & fName3, dir + "products\" & dirName & "\" & fName3)
                File.Copy("d:\<removed for security reasons>\cfolder\" & fName4, dir + "products\" & dirName & "\" & fName4)
            End While
 
            xmlr.Close()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

Open in new window

.NET ProgrammingASP.NETXML

Avatar of undefined
Last Comment
McExp

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
McExp

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
McExp

The function calls itself until it runs out of nodes. all you need do beyond this is add your aditional logic
SOLUTION
nmarun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
McExp

Happy?

do you need further assistance?
McExp

Accept the Answer, Split the points 70/30 between me and nmarn
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck