Solved

How to search and delete a record in an xml file

Posted on 2016-07-27
3
48 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now