Solved

How to search and delete a record in an xml file

Posted on 2016-07-27
3
51 Views
Last Modified: 2016-07-27
Hi Experts,

I have a xml file. I do not want duplicate barcodes. How can I search for a barcode.  Suppose in textbox I entered  'test'. Then It should search the file.  Also how to delete a particular ID. For example- suppose I have the ID- 1, then how can I delete it from the file.

Thanks in advance.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<XMLDataSet xmlns="ALLBARCODES">
  <Barcodes>
    <ID>0</ID>
    <Barcode>test</Barcode>
  </Barcodes>
  <Barcodes>
    <ID>1</ID>
    <Barcode>test1</Barcode>
  </Barcodes>
  <Barcodes>
    <ID>2</ID>
    <Barcode>Test3</Barcode>
  </Barcodes>
</XMLDataSet>
0
Comment
Question by:RadhaKrishnaKiJaya
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41731830
Hi RadhaKrishnaKiJaya;

This should do what you want.
// The XML file to be searched or modified
var filename = "C:/Working Directory/MyData.xml";
XDocument xdoc = XDocument.Load(filename);
XNamespace ns = xdoc.Root.Name.NamespaceName;
// ToLower will make the search case insensitive
var find = textBox1.Text.ToLower();

// Query for a barcode to see if it has duplicates in the file
var results = xdoc.Root.Descendants(ns + "Barcode")
                  .Where(bc => bc.Value.ToLower() == find)
                  .Select(bc => bc.Parent).ToList();
                  
// Query for a ID and delete it from the file
// ID to be deleted
var toDelete = "1";
// This query will throw an exception if more then one ID is found
// So you will need to test for it.
XElement deleteID = xdoc.Root.Descendants(ns + "ID")
                        .Where(d => d.Value == toDelete)
                        .Select(d => d.Parent)
                        .SingleOrDefault();

// This will remove the complete node from the xdoc document
deleteID.Remove();
// You will need to save xdoc to the file system to persist the modification of the document
xdoc.Save(filename);

Open in new window

0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41731988
Thank you very much. It is working perfectly.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41732016
Not a problem RadhaKrishnaKiJaya; glad I was able to help.
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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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