Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 111
  • Last Modified:

Help with deleting data in xml file

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
vcharles
Asked:
vcharles
  • 8
  • 4
  • 3
2 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
What is the structure of the XML, and what denotes a "record" in your document?
0
 
vcharlesAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vcharlesAuthor Commented:
Hi,

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

Thanks,

Victor
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
Fernando SotoRetiredCommented:
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
 
vcharlesAuthor Commented:
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
 
vcharlesAuthor Commented:
Hi,

I will try your code and get back to you.

Thanks for converting to VB.NET,

Victor
0
 
Fernando SotoRetiredCommented:
So all XML files in the folder/s have the table names as <TableNSC />?
0
 
vcharlesAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
vcharlesAuthor Commented:
Thank you both for your solutions will run a test and get back to you.
0
 
vcharlesAuthor Commented:
Thank You!
0
 
vcharlesAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now