Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with deleting data in xml file

Posted on 2014-11-19
15
Medium Priority
?
109 Views
Last Modified: 2014-11-22
Hi,


I have multiple xml files in multiple folders in my application's "Data" sub folder. How do I loop through each folder and delete all records in the xml files except for records with <ID>0</ID>. I am trying the clear values in xml files in multiple folders.

Thanks,

Victor
0
Comment
Question by:vcharles
  • 8
  • 4
  • 3
15 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40452439
What is the structure of the XML, and what denotes a "record" in your document?
0
 

Author Comment

by:vcharles
ID: 40452609
Hi,

The structure looks like the following:

<Root>
<TableNSC>
<ID>0</ID>
<ItemA></ItemA>
<ItemA></ItemA>
<ItemA></ItemA>
</TableNSC>
<TableNSC>
<ID></ID>
<ItemA>1</ItemA>
<ItemA></ItemA>
<ItemA></ItemA>
</TableNSC>
<TableNSC>
<ID>2</ID>
<ItemA></ItemA>
<ItemA></ItemA>
<ItemA></ItemA>
</TableNSC>
</Root>

Victor
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40452651
Assuming your XML structures are all the same you could try:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string rootDirectory = @"C:\temp\xml";

            var files = from file in Directory.GetFiles(rootDirectory, "*.xml", SearchOption.AllDirectories)
                        select file;

            foreach(var file in files)
            {
                XDocument doc = XDocument.Load(file);
                var nodes = from node in doc.Descendants("TableNSC")
                            where node.Element("ID").Value != "0"
                            select node;

                nodes.Remove();
                doc.Save(file);
            }
        }
    }
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:vcharles
ID: 40452809
Hi,

Can you please send me the solution in VB.NET?.

Thanks,

Victor
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40452962
Sorry, thought i'd got here from a C# topic for some reason.

VB version below:
Imports System.Data
Imports System.Linq
Imports System.Xml.Linq
Imports System.IO

Module Module1

    Sub Main()

        Dim rootDirectory As String = "C:\temp\xml"

        Dim files = From file In Directory.GetFiles(rootDirectory, "*.xml", SearchOption.AllDirectories)
                    Select file

        For Each file In files
            Dim doc As XDocument = XDocument.Load(file)
            Dim nodes = From node In doc.Descendants("TableNSC")
                        Where node.Element("ID") <> "0"
                        Select node

            nodes.Remove()
            doc.Save(file)
        Next

    End Sub

End Module

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40453233
Hi Victor;

Are all the table names in the XML document in the folder have the node name TableNSC or is it more like TableXXX where XXX is some other characters?
0
 

Author Comment

by:vcharles
ID: 40453269
Hi Fernando,

It is more like TableXXX, for example I have a folder named MasterNSC and files with name LinkFinalNSCUSA.xml,  LinkFinalNSCBEL.xml,and the table name for both files is NSC.

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 40453277
Hi,

I will try your code and get back to you.

Thanks for converting to VB.NET,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40453285
So all XML files in the folder/s have the table names as <TableNSC />?
0
 

Author Comment

by:vcharles
ID: 40453302
No,  table NSC is only for folder name MasterNSC, I also have folders such as MasterNSN, MasterFIF etc.. where tables names are NSN, FIF.
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 40453442
Hi Victory;

See if this is what you are looking for.

'' 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

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 40453447
You will want to be slightly more general with the selection then.
Change the code to:
Imports System.Data
Imports System.Linq
Imports System.Xml.Linq
Imports System.IO

Module Module1

    Sub Main()

        Dim rootDirectory As String = "C:\temp\xml"

        Dim files = From file In Directory.GetFiles(rootDirectory, "*.xml", SearchOption.AllDirectories)
                    Select file

        For Each file In files
            Dim doc As XDocument = XDocument.Load(file)
            Dim nodes = From node In doc.Root.Elements()
                        Where node.Element("ID").Value <> "0"
                        Select node

            nodes.Remove()
            doc.Save(file)
        Next

    End Sub

End Module

Open in new window

0
 

Author Comment

by:vcharles
ID: 40453501
Thank you both for your solutions will run a test and get back to you.
0
 

Author Closing Comment

by:vcharles
ID: 40460330
Thank You!
0
 

Author Comment

by:vcharles
ID: 40460331
Hi,

How do you modify the code to only apply  to files which end with a particular country (i.e. LinkTemUSA.xml). I also need to clear data for a specific country rather than all the files in the folder. The country will be selected from a combobox.

Thanks,

Victor
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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:…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

916 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