Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

What is the structure of the XML, and what denotes a "record" in your document?
Avatar of Victor  Charles

ASKER

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

Hi,

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

Thanks,

Victor
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

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?
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
Hi,

I will try your code and get back to you.

Thanks for converting to VB.NET,

Victor
So all XML files in the folder/s have the table names as <TableNSC />?
No,  table NSC is only for folder name MasterNSC, I also have folders such as MasterNSN, MasterFIF etc.. where tables names are NSN, FIF.
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both for your solutions will run a test and get back to you.
Thank You!
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