Victor Charles
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
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
What is the structure of the XML, and what denotes a "record" in your document?
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
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);
}
}
}
}
ASKER
Hi,
Can you please send me the solution in VB.NET?.
Thanks,
Victor
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:
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
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?
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?
ASKER
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
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
ASKER
Hi,
I will try your code and get back to you.
Thanks for converting to VB.NET,
Victor
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 />?
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both for your solutions will run a test and get back to you.
ASKER
Thank You!
ASKER
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
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