Solved

Help with deleting data in xml file

Posted on 2014-11-19
15
101 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 63

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 63

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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 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 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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