Solved

Help with deleting data in xml files based on filename

Posted on 2014-11-24
2
66 Views
Last Modified: 2014-12-03
Hi,

How do you modify the code below to delete data in all files ending with Country.xml? where country will be selected from a combobox. For example filename is LinkFileUSA.xml

'' The main directory that holds all the other directories
'' Modify the directory path to fit your needs
Dim mainDirPath As String = "C:\Working Directory\temp\"
'' Get an array of directories that you will look into 
'' to modify the XML files
Dim dirs = Directory.GetDirectories(mainDirPath)

'' Iterate through all the directories
For Each thisDir In dirs
    '' Select all the XML files and modify them
    For Each xmlFile In Directory.GetFiles(thisDir, "*.xml")
        '' Open each document
        Dim xdoc As XDocument = XDocument.Load(xmlFile)
        '' Query the document and find all elements that are not "ID' = to 0
        Dim tables As List(Of XElement) = (From ele In xdoc.Descendants()
                                           Where ele.Name.LocalName.StartsWith("Table") AndAlso ele.Element("ID").Value <> "0"
                                           Select ele).ToList()

        '' Remove all unwanted elements from the main document
        tables.Remove()
        '' Save the modified file back to the file system
        xdoc.Save(xmlFile)
    Next
Next

Open in new window



Thanks,

victor
0
Comment
Question by:vcharles
2 Comments
 
LVL 9

Accepted Solution

by:
teebon earned 500 total points
ID: 40463750
Hi Victor,

Try the following:

Dim countryString As String = DropDownList.SelectedItem.Value

Dim mainDirPath As String = "C:\Working Directory\temp\"
Dim dirs = Directory.GetDirectories(mainDirPath)

For Each thisDir In dirs
    For Each xmlFile In Directory.GetFiles(thisDir,   "*" + countryString + ".xml")

' The rest follows your code

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 40478534
Thank You.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question