Solved

How to search and delete a record in an xml file

Posted on 2016-07-27
3
58 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

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 63

Expert Comment

by:Fernando Soto
ID: 41732016
Not a problem RadhaKrishnaKiJaya; glad I was able to help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

763 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